日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

〖Demo〗-- HAproxy配置文件操作

發布時間:2025/3/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 〖Demo〗-- HAproxy配置文件操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【HAproxy配置文件操作】

要求

1. 根據用戶輸入輸出對應的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可刪除backend 和sever信息
5. 操作配置文件前進行備份
6 添加server信息時,如果ip已經存在則修改;如果backend不存在則創建;若信息與已有信息重復則不操作

1 def find(backend): 2 ''' 3 查看backend下sever信息 4 :param backend: 5 :return: 6 ''' 7 ls = [] 8 with open('HA.txt', 'r') as f: 9 T = True 10 for line in f: 11 # 找到第一個backend,做記錄 T = False 12 if line.strip() == 'backend %s' % (backend): 13 T = False 14 continue 15 # 找到第二個backend,做記錄 T = True 16 if T == False and line.strip().startswith('backend'): 17 T = True 18 break 19 # 把關于False的加入到列表中 20 if T == False and line.strip(): 21 ls.append(line.strip()) 22 return ls 23 24 25 def check_bk(): 26 ''' 27 列出backend的信息 28 :return: 選中的backend 29 ''' 30 ls = [] 31 T = True 32 with open('HA.txt', 'r') as f1: 33 for line in f1: 34 if line.strip().startswith('backend') == True: 35 to = line.split(' ') 36 ls.append(to[1]) 37 for i, v in enumerate(ls,1): 38 print (i, v) 39 while T: 40 t1 = input('請輸入查看的server的 backend信息') 41 if t1.isdecimal() and int(t1) in range(0,len(ls)+1): 42 a = ls[int(t1)-1] 43 break 44 else: 45 print('輸入有誤,重新輸入') 46 return a 47 48 def backup(): 49 ''' 50 備份 51 :return: 52 ''' 53 with open('HA.txt', 'r') as f1, open('HA_old.txt', 'w') as f2: 54 for line in f1: 55 f2.write(line) 56 57 58 def add(bk, sr): 59 ''' 60 添加記錄 61 :param bk: 62 :param sr: 63 :return: 64 ''' 65 r_list = find(bk) 66 # 沒有關于bk的記錄,直接添加內容 67 T = True 68 a = 'backend %s' % bk 69 backup() 70 if not r_list: 71 with open('HA.txt', 'a') as f1: 72 f1.write('\n%s\n'% a) 73 f1.write(" %s\n" % sr) 74 # 有關于bk的記錄 75 else: 76 # 沒有sever記錄 77 backup() 78 if sr not in r_list: 79 with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2: 80 for line in f1: 81 if line.strip() == 'backend %s' % (bk): 82 T = False 83 f2.write(line) 84 continue 85 if T == False and line.strip().startswith('backend'): 86 T = True 87 if T == True: 88 f2.write(line) 89 else: 90 if sr not in r_list: 91 r_list.append(sr) 92 t = '\n '.join(r_list) 93 f2.write(' '* 8 + t + '\n\n') 94 # 有sever記錄,直接復制 95 else: 96 print('記錄已存在,請重新選擇') 97 caidan() 98 99 100 def update_bk (): 101 ''' 102 需要修改或刪除backend 103 :return: 104 ''' 105 backup() 106 T = True 107 bk = check_bk() 108 ls =[] 109 with open('HA.txt', 'r') as f1: 110 for line in f1: 111 if line.strip().startswith('backend') == True: 112 to = line.split(' ') 113 ls.append(to[1].strip()) 114 c = input(''' 115 請選擇需要選項: 116 1 修改backend 117 2 刪除backend 118 ''') 119 while T: 120 #修改backend信息 121 if c == '1': 122 bk1 = input('請輸入修改后的backend信息') 123 t3 = input('信息確認請按 y, 重新輸入請按其他鍵') 124 if bk1 in ls: 125 print('信息已存在,請重新選擇') 126 break 127 else: 128 if t3.lower() == 'y': 129 with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2: 130 for line in f1: 131 #需要修改的backend 132 if line.strip() == 'backend %s' % (bk.strip()): 133 f2.write('backend %s \n' % (bk1)) 134 else: 135 f2.write(line) 136 print('修改成功') 137 break 138 else: 139 continue 140 #刪除backend信息 141 elif c == '2': 142 t = find(bk.strip()) 143 while T: 144 with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2: 145 for line in f1: 146 if line.strip() == 'backend %s' % (bk.strip()): 147 continue 148 if line.strip() in t: 149 continue 150 else: 151 f2.write(line) 152 print('刪除成功') 153 break 154 break 155 156 def check_sr(): 157 T1 = True 158 while T1: 159 cont = 0 160 a1 = input('sever:') 161 ti = a1.split('.') 162 for ii in ti: 163 if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4: 164 cont = cont + 1 165 continue 166 else: 167 print('輸入有誤,請輸入由四組0-255組成的IP地址') 168 break 169 if cont == 4: 170 break 171 return a1 172 173 174 def update_sr(): 175 ''' 176 修改或刪除sever 177 :return: 178 ''' 179 t = check_bk() 180 backup() 181 T = True 182 T1 = True 183 ls = find(t.strip()) 184 cc = 0 185 with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2: 186 for line in f1: 187 if line.strip() == 'backend %s' %(t.strip()): 188 T = False 189 f2.write(line) 190 continue 191 if T == False and line.strip().startswith('backend'): 192 T = True 193 if T == True: 194 if line.strip() != cc and line.strip() not in ls: 195 f2.write(line) 196 197 else: 198 for i, v in enumerate(ls, 1): 199 print ('%s: %s' % (i, v)) 200 while T1: 201 v1 = input('請輸入要修改或刪除的編號') 202 if v1.isdecimal() and int(v1) in range(len(ls)+1): 203 break 204 else: 205 print('輸入有誤,請重新輸入') 206 continue 207 x = input('請選擇 1 = 修改 其他鍵 = 刪除') 208 if x == '1': 209 while T1: 210 a1 = check_sr() 211 a2 = input('weight:') 212 a3 = input('maxconn:') 213 if a2.isdecimal() and a3.isdecimal(): 214 t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3) 215 print('backend: %s \n%s' % (t, t1)) 216 else: 217 print('所輸入weight 和 maxconn 必須為數字,請重新輸入') 218 continue 219 li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3) 220 # server 221 if li_sr in ls: 222 tt = input('server已存在,請按 y = 重新輸入,或按 other key = 返回') 223 if tt == 'y': 224 continue 225 else: 226 break 227 else: 228 a4 = input('請確認信息正確,y = 確認, other key = 重新輸入') 229 230 if a4 == 'y': 231 f2.write(' '*8 + li_sr +'\n') 232 else: 233 continue 234 print('修改成功') 235 break 236 cc = ls[int(v1)-1] 237 del ls[int(v1)-1] 238 for tt in ls: 239 f2.write(' '*8 + tt +'\n') 240 T = True 241 else: 242 print('刪除成功') 243 cc = ls[int(v1)-1] 244 del ls[int(v1)-1] 245 for tt in ls: 246 f2.write(' '*8 + tt +'\n') 247 T = True 248 249 250 def caidan (): 251 T = True 252 while T: 253 ls = input('''請輸入所需編號: 254 1 查看backend下sever信息 255 2 添加記錄 256 3 修改或刪除backend 257 4 修改或刪除sever 258 5 退出 259 ''') 260 if ls == '1': 261 i = check_bk() 262 l1 = find(i.strip()) 263 for a in l1: 264 print (a) 265 elif ls == '2': 266 while T: 267 t = input('請輸入backend信息') 268 print('請輸入sever信息') 269 cont = 0 270 while T: 271 a1 = input('sever:') 272 ti = a1.split('.') 273 for ii in ti: 274 if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4: 275 cont = cont + 1 276 continue 277 else: 278 print('輸入有誤,請輸入由四組0-255組成的IP地址') 279 break 280 if cont == 4: 281 break 282 while T: 283 a2 = input('weight:') 284 a3 = input('maxconn:') 285 if a2.isdecimal() and a3.isdecimal(): 286 t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3) 287 print('backend: %s \n%s' % (t, t1)) 288 break 289 else: 290 print('所輸入weight 和 maxconn 必須為數字,請重新輸入') 291 continue 292 t3 = input('信息確認請按 y, 重新輸入請按其他鍵') 293 if t3.lower() == 'y': 294 add (t, t1) 295 print('添加成功') 296 break 297 else: 298 continue 299 elif ls == '3': 300 update_bk() 301 elif ls == '4': 302 update_sr() 303 elif ls == '5': 304 exit() 305 else: 306 print('輸入有誤,請重新輸入') 307 308 caidan()

?

轉載于:https://www.cnblogs.com/SHENGXIN/p/8088726.html

總結

以上是生活随笔為你收集整理的〖Demo〗-- HAproxy配置文件操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。