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 ...
随机推荐
- expected identifier before numeric constant
症状: expected identifier before numeric constantexpected `}' before numeric constantexpected unqualif ...
- 10-8位7段数码管驱动实验——小梅哥FPGA设计思想与验证方法视频教程配套文档
芯航线--普利斯队长精心奉献 实验目的: 1.实现FPGA驱动数码管动态显示: 2.使用In system sources and probes editor工具,输入需要显示在数码管上的的数据, ...
- 深入理解javascript原型和闭包(完结)
原文链接:http://www.cnblogs.com/wangfupeng1988/p/3977924.html 说明: 该教程绕开了javascript的一些基本的语法知识,直接讲解javascr ...
- typedef 函数指针 数组 std::function
1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA, *PDATA; //D ...
- ffmpeg centos6.5上安装(测试 amr 转换为 mp3)
1. 下载相关源码包 wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz wget http://sourcefo ...
- 前端面试-----JavaScript题
用面试题,复习一下,js基础. 1.综合题 function Foo() { getName = function () { alert (1); }; return this; } Foo.getN ...
- IoC、DI、AOP
相信学习Java语言的同学都对这三个概念不太陌生.下面用spring的例子简单说明这三个概念. IoC(Inversion of Control):控制反转.正常情况(控制未反转)下,如果servic ...
- 【前端】提取URL中的各个GET参数
/**************************** * 有这样一个URL:http://item.taobao.com/item.htm?a=1&b=2&c=&d=xx ...
- ImageLoader图片加载
http://blog.csdn.net/liu1164316159/article/details/38728259 转载请注明http://write.blog.csdn.net/po ...
- Excel的数据导入到PB的DW中
Excel的数据导入到PB的DW中//==================================================================== // Event:cb_ ...