HAproxy配置文件操作
要求
1. 根据用户输入输出对应的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可删除backend 和sever信息
5. 操作配置文件前进行备份
6 添加server信息时,如果ip已经存在则修改;如果backend不存在则创建;若信息与已有信息重复则不操作
- def find(backend):
- '''
- 查看backend下sever信息
- :param backend:
- :return:
- '''
- ls = []
- with open('HA.txt', 'r') as f:
- T = True
- for line in f:
- # 找到第一个backend,做记录 T = False
- if line.strip() == 'backend %s' % (backend):
- T = False
- continue
- # 找到第二个backend,做记录 T = True
- if 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 = True
- a = '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 = False
- f2.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 = True
- bk = 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 == '':
- 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:
- #需要修改的backend
- if line.strip() == 'backend %s' % (bk.strip()):
- f2.write('backend %s \n' % (bk1))
- else:
- f2.write(line)
- print('修改成功')
- break
- else:
- continue
- #删除backend信息
- elif c == '':
- 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 = 0
- 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 + 1
- continue
- else:
- print('输入有误,请输入由四组0-255组成的IP地址')
- break
- if cont == 4:
- break
- return a1
- def update_sr():
- '''
- 修改或删除sever
- :return:
- '''
- t = check_bk()
- backup()
- T = True
- T1 = True
- ls = find(t.strip())
- cc = 0
- with 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 = False
- f2.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('输入有误,请重新输入')
- continue
- x = input('请选择 1 = 修改 其他键 = 删除')
- if x == '':
- 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 必须为数字,请重新输入')
- continue
- li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
- # server
- if 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:
- continue
- print('修改成功')
- break
- cc = 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 == '':
- i = check_bk()
- l1 = find(i.strip())
- for a in l1:
- print (a)
- elif ls == '':
- while T:
- t = input('请输入backend信息')
- print('请输入sever信息')
- cont = 0
- while 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 + 1
- continue
- 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 必须为数字,请重新输入')
- continue
- t3 = input('信息确认请按 y, 重新输入请按其他键')
- if t3.lower() == 'y':
- add (t, t1)
- print('添加成功')
- break
- else:
- continue
- elif ls == '':
- update_bk()
- elif ls == '':
- update_sr()
- elif ls == '':
- exit()
- else:
- print('输入有误,请重新输入')
- 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 = False
if line.strip() == 'backend %s' % (backend):
T = False
continue
# 找到第二个backend,做记录 T = True
if 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[])
for i, v in enumerate(ls,):
print (i, v)
while T:
t1 = input('请输入查看的server的 backend信息')
if t1.isdecimal() and int(t1) in range(,len(ls)+):
a = ls[int(t1)-]
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 = True
a = '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 = False
f2.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 = True
bk = 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[].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:
#需要修改的backend
if 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 = 0
a1 = input('sever:')
ti = a1.split('.')
for ii in ti:
if ii.isdecimal() and int(ii) in range(, ) and len(ti) == :
cont = cont + 1
continue
else:
print('输入有误,请输入由四组0-255组成的IP地址')
break
if cont == :
break
return a1- def update_sr():
'''
修改或删除sever
:return:
'''
t = check_bk()
backup()
T = True
T1 = True
ls = find(t.strip())
cc = 0
with 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 = False
f2.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, ):
print ('%s: %s' % (i, v))
while T1:
v1 = input('请输入要修改或删除的编号')
if v1.isdecimal() and int(v1) in range(len(ls)+):
break
else:
print('输入有误,请重新输入')
continue
x = 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 必须为数字,请重新输入')
continue
li_sr = 'server %s %s weight %s maxconn %s' % (a1, a1, a2, a3)
# server
if 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:
continue
print('修改成功')
break
cc = ls[int(v1)-]
del ls[int(v1)-]
for tt in ls:
f2.write(' '*8 + tt +'\n')
T = True
else:
print('删除成功')
cc = ls[int(v1)-]
del ls[int(v1)-]
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 = 0
while T:
a1 = input('sever:')
ti = a1.split('.')
for ii in ti:
if ii.isdecimal() and int(ii) in range(, ) and len(ti) == :
cont = cont + 1
continue
else:
print('输入有误,请输入由四组0-255组成的IP地址')
break
if cont == :
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 必须为数字,请重新输入')
continue
t3 = 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()
HAproxy配置文件操作的更多相关文章
- python之haproxy配置文件操作(第三天)
作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_py ...
- python3 haproxy配置文件操作练习
哈哈 老规矩 先来一个NLP第六条:咳咳! 六,重复旧的做法,只会得到旧的结果 做法有不同,结果才会有不同. 如果,你的事没有结果,改变你的做法.任何新的做法,都比旧的多一份成功的机会. 想明天比 ...
- python作业:HAproxy配置文件操作(第三周)
一.作业需求: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend ...
- 第三周作业HAproxy文件操作
#coding:utf-8 #Author:Mr Zhi """ HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添 ...
- Python3.5 day3作业二:修改haproxy配置文件。
需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 lo ...
- haproxy配置文件简单管理
版本:python3功能:对haproxy配置文件进行简单的查询.添加以及删除功能操作流程:1.根据提示选择相应的选项2.进入所选项后,根据提示写入相应的参数3.查询功能会返回查询结果,添加.删除以及 ...
- s12-day03-work01 python修改haproxy配置文件(初级版本)
#!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo.song ...
- haproxy 配置文件详解 之 综述
HAProxy 配置文件根据功能和用途,主要有5 个部分组成,但有些部分并不是必须的,可以根据需要选择相应的部分进行配置. 1.global 部分 用来设定全局配置参数,属于进程级的配置,通常和操作系 ...
- 配置文件操作(ini、cfg、xml、config等格式)
配置文件的格式主要有ini.xml.config等,现在对这些格式的配置文件的操作(C#)进行简单说明. INI配置文件操作 调用系统函数GetPrivateProfileString()和Write ...
随机推荐
- webstorm--破解
2016.2.2 版本的破解方式: 安装以后,打开软件会弹出一个对话框:选择"license server" 输入:http://114.215.133.70:41017 2016 ...
- busybox的编译、使用及安装
转载于:http://blog.sina.com.cn/wyw1976 busybox是什么? (1)busybox是Linux上的一个应用程序(application),即只有一个ELF文件头. ( ...
- 《编写可维护的JavaScript》——JavaScript编码规范(六)
变量.函数和运算符 在讨论过基本的JavaScript书写格式化之后,接下来关注如何使用函数.变量和运算符来减少复杂度和增强可读性就显得十分重要了. 变量声明 变量声明是通过var语句来完成的.var ...
- org.hibernate.AssertionFailure:collection[......] was not processed by flush()
八月 12, 2016 11:00:49 上午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() f ...
- 封装好的AFN网络请求框架和MBProgress
demo:https://github.com/IMCCP/CCPAFNNetworking(收藏下来)
- 2017年1月2日 星期一 --出埃及记 Exodus 21:28
2017年1月2日 星期一 --出埃及记 Exodus 21:28 "If a bull gores a man or a woman to death, the bull must be ...
- 统计图表类型选择应用总结&表数据挖掘方法及应用
数据挖掘方法及应用: 图表注意事项 • 信息完整:图表标题.单位.图例.脚注.来源等 • 避免无意义的图表 • 一表反映一个观点 • 只选对的不选复杂的图表 • 标题一句话阐述清楚反映观点 确定对比关 ...
- /usr/include/features.h:367:25:fatal errorXXXXXX类似这种问题
解决方案: sudo apt-get install g++=multilib //至于为什么还没搞清楚,搞清楚在写上来吧!
- boost学习笔记(七)---date_time库
date_time库的日期基于格里高利历,支持从1400-01-01到9999-12-31之间的日期计算 #define BOOST_DATE_TIME_SOURCE #include <boo ...
- ReportDesigner中实现保存,保存所有,注册ICommandHandler方式实现
示例: https://www.devexpress.com/Support/Center/Example/Details/E4354