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()
def find(backend):''' 查看backend下sever信息:param backend::return: '''ls = []with open('HA.txt', 'r') as f: T = True for line in f:# 找到第一个backend,做记录 T = Falseif line.strip() == 'backend %s' % (backend): T = False continue# 找到第二个backend,做记录 T = Trueif T == False and line.strip().startswith('backend'):T = True break# 把关于False的加入到列表中if T == False and line.strip(): ls.append(line.strip())return ls def check_bk():''' 列出backend的信息:return: 选中的backend '''ls = [] T = True with open('HA.txt', 'r') as f1:for line in f1:if line.strip().startswith('backend') == True: to = line.split(' ') ls.append(to[1])for i, v in enumerate(ls,1):print (i, v)while T: t1 = input('请输入查看的server的 backend信息')if t1.isdecimal() and int(t1) in range(0,len(ls)+1): a = ls[int(t1)-1]break else:print('输入有误,重新输入')return a def backup():''' 备份:return: '''with open('HA.txt', 'r') as f1, open('HA_old.txt', 'w') as f2:for line in f1: f2.write(line) def add(bk, sr):''' 添加记录:param bk::param sr::return: '''r_list = find(bk)# 没有关于bk的记录,直接添加内容T = Truea = 'backend %s' % bk backup()if not r_list:with open('HA.txt', 'a') as f1: f1.write('\n%s\n'% a) f1.write(" %s\n" % sr)# 有关于bk的记录else:# 没有sever记录backup()if sr not in r_list:with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:for line in f1:if line.strip() == 'backend %s' % (bk): T = Falsef2.write(line)continue if T == False and line.strip().startswith('backend'): T = True if T == True: f2.write(line)else:if sr not in r_list: r_list.append(sr) t = '\n '.join(r_list) f2.write(' '* 8 + t + '\n\n')# 有sever记录,直接复制else:print('记录已存在,请重新选择') caidan() def update_bk ():''' 需要修改或删除backend:return: '''backup() T = Truebk = check_bk() ls =[]with open('HA.txt', 'r') as f1:for line in f1:if line.strip().startswith('backend') == True: to = line.split(' ') ls.append(to[1].strip()) c = input(''' 请选择需要选项: 1 修改backend 2 删除backend ''')while T:#修改backend信息if c == '1': bk1 = input('请输入修改后的backend信息') t3 = input('信息确认请按 y, 重新输入请按其他键')if bk1 in ls:print('信息已存在,请重新选择')break else:if t3.lower() == 'y':with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:for line in f1:#需要修改的backendif line.strip() == 'backend %s' % (bk.strip()): f2.write('backend %s \n' % (bk1))else: f2.write(line)print('修改成功')break else:continue#删除backend信息elif c == '2': t = find(bk.strip())while T:with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:for line in f1:if line.strip() == 'backend %s' % (bk.strip()):continue if line.strip() in t:continue else: f2.write(line)print('删除成功')break break def check_sr(): T1 = True while T1: cont = 0a1 = input('sever:') ti = a1.split('.')for ii in ti:if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4: cont = cont + 1continue else:print('输入有误,请输入由四组0-255组成的IP地址')break if cont == 4:break return a1 def update_sr():''' 修改或删除sever:return: '''t = check_bk() backup() T = TrueT1 = Truels = find(t.strip()) cc = 0with open('HA_old.txt', 'r') as f1, open('HA.txt', 'w') as f2:for line in f1:if line.strip() == 'backend %s' %(t.strip()): T = Falsef2.write(line)continue if T == False and line.strip().startswith('backend'): T = True if T == True:if line.strip() != cc and line.strip() not in ls: f2.write(line) else:for i, v in enumerate(ls, 1):print ('%s: %s' % (i, v))while T1: v1 = input('请输入要修改或删除的编号')if v1.isdecimal() and int(v1) in range(len(ls)+1):break else:print('输入有误,请重新输入')continuex = input('请选择 1 = 修改 其他键 = 删除')if x == '1':while T1: a1 = check_sr() a2 = input('weight:') a3 = input('maxconn:')if a2.isdecimal() and a3.isdecimal(): t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)print('backend: %s \n%s' % (t, t1))else:print('所输入weight 和 maxconn 必须为数字,请重新输入')continueli_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)# serverif li_sr in ls: tt = input('server已存在,请按 y = 重新输入,或按 other key = 返回')if tt == 'y':continue else:break else: a4 = input('请确认信息正确,y = 确认, other key = 重新输入') if a4 == 'y': f2.write(' '*8 + li_sr +'\n')else:continueprint('修改成功')breakcc = ls[int(v1)-1]del ls[int(v1)-1]for tt in ls: f2.write(' '*8 + tt +'\n') T = True else:print('删除成功') cc = ls[int(v1)-1]del ls[int(v1)-1]for tt in ls: f2.write(' '*8 + tt +'\n') T = True def caidan (): T = True while T: ls = input('''请输入所需编号: 1 查看backend下sever信息 2 添加记录 3 修改或删除backend 4 修改或删除sever 5 退出 ''')if ls == '1': i = check_bk() l1 = find(i.strip())for a in l1:print (a)elif ls == '2':while T: t = input('请输入backend信息')print('请输入sever信息') cont = 0while T: a1 = input('sever:') ti = a1.split('.')for ii in ti:if ii.isdecimal() and int(ii) in range(0, 255) and len(ti) == 4: cont = cont + 1continue else:print('输入有误,请输入由四组0-255组成的IP地址')break if cont == 4:break while T: a2 = input('weight:') a3 = input('maxconn:')if a2.isdecimal() and a3.isdecimal(): t1 = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)print('backend: %s \n%s' % (t, t1))break else:print('所输入weight 和 maxconn 必须为数字,请重新输入')continuet3 = input('信息确认请按 y, 重新输入请按其他键')if t3.lower() == 'y': add (t, t1)print('添加成功')break else:continue elif ls == '3': update_bk()elif ls == '4': update_sr()elif ls == '5':exit()else:print('输入有误,请重新输入') caidan()
转载于:https://www.cnblogs.com/nikitapp/p/6027616.html
HAproxy配置文件操作相关推荐
- 〖Demo〗-- HAproxy配置文件操作
[HAproxy配置文件操作] 要求 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. ...
- 修改haproxy配置文件
HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除b ...
- python操作haproxy配置文件实例
2019独角兽企业重金招聘Python工程师标准>>> 配置文件模板: defaultslog globalmode httpoption httpcloseoption dontl ...
- CENTOS7 高性能Linux集群 通过yum进行 haproxy配置 !安装 !使用!HAProxy配置文件详解
Haproxy配置 永久修改名字:便于区分虚拟机! Haproxy:hostnamectl set-hostname haproxy Web1: hostnamectl set-hostname WE ...
- php程序里的configini_php中配置文件操作 如config.php文件的读取修改等操作
代码如下: $name="admin";//kkkk $bb='234'; $db=4561321; $kkk="admin"; ?> 函数定义: 配置文 ...
- haproxy配置文件
haproxy配置文件 vim haproxy.cfg #--------------------------------------------------------------------- # ...
- java properties配置文件_java properties配置文件操作
实现运用Java.util.Properties来进行对.properties配置文件操作. 配置文件实例:如debug.properties #Tue Mar 21 15:46:17 CST 201 ...
- java过去配置文件的值_java对.properties配置文件操作
实现运用Java.util.Properties来进行对.properties配置文件操作. 配置文件实例:如debug.properties #Tue Mar 21 15:46:17 CST 201 ...
- python读取配置文件获取所有键值对_Python读取properties配置文件操作示例
本文实例讲述了Python读取properties配置文件操作.分享给大家供大家参考,具体如下: 工作需要将Java项目的逻辑改为python执行,Java的很多配置文件都是.properties的, ...
- Config配置文件操作
一.使用说明: // 编辑web.config文件 // 打开配置文件 Configuration config = System.Web.Configuration. ...
最新文章
- 在两幅图像之间获得正确(最小二乘意义)仿射变换参数affine points via least square method
- 传入json对象_【Python基础】可迭代对象amp;迭代器对象及其实现
- win10系统下安装mysql
- linux ntfs 速度慢,将U盘磁盘格式改成NTFS解决u盘复制速度慢问题
- SqlServer 时间格式化
- 调试利器:浏览器 Logger
- MFC日志(2011.4.9)
- python--字符串为空
- 谷歌:修复0day漏洞的平均耗时比3年前减少28天
- 剑指offer题目系列一
- javascript数据类型(摘录)
- linux sh指令自动验证密码,shell脚本自动输入用户名和密码更新服务器上的git项目...
- 181006扇贝有道每日一句
- BootLoad中上位机与下位机交互
- Redis的配置文件详解
- git rebase 理解
- 第四章 智能合约 [20]
- C#支付宝支付接口H5版(手机网页支付)
- c语言 cdma编码正交的8位码片,CDMA编码
- 西南民族大学第十届校赛(同步赛)(J题——怪盗基德 月之瞳宝石)