Warning: Array to string conversion in /home/wwwroot/myf5/wp-content/plugins/wpjam-basic/public/wpjam-utils.php on line 496
这是很早前写的一个样例,基于业务模板,系统自动化创建业务配置
业务输入模板(可以通过任何形式产生,例如一个自服务的portal website):
createvs.txt createpool.txt createmonitor.txt
中间程序F5_Deploy.py, 中间输出 tmsh.txt (作为ssh.py的输入)
(如果系统支持iControlRest,可以直接利用Python执行REST)
自动执行ssh.py
###Tested on v11.6###
#only support tcp,http,icmp monitor now
## Created by WWW.MYF5.NET @2015
def createmonitor():
try:
f=file('createmonitor.txt')
lines=f.readlines()
print lines
l=[]
for line in lines:
print line
#get value of each line
linedata=line.split(",")[1]
#remove"\n"
i=linedata.split("\n")[0]
print i
l.append(i)
print l
if l[6]=="enabled":
if l[0]=="icmp" and l[7]!="" and l[7]!="*":
#if it is icmp monitor, only need ip and it has an valid ip
tr="transparent enabled destination %s"%l[7]
else:
#for other monitors, ip and port must be there
if l[7]=="" or l[7]=="*" or l[8]=="" or l[8]=="*":
print "Error: With transparent enabled, must have ip and port"
return
else:
tr="transparent enabled destination %s:%s"%(l[7],l[8])
else:
#not transparent, could set alias, f5 only support *:* or *:port or ip:port format
if l[0]=="icmp":
#icmp monitor,cant have port setting
if l[7]=="" or l[7]=="*":
#default setting
tr=""
else:
tr="destination %s"%l[7]
else:
#not icmp,support *:* or *:port or ip:port
if (l[7]=="" or l[7]=="*") and (l[8]=="" or l[8]=="*"):
#default setting
tr=""
elif l[7]!="" and l[7]!="*" and (l[8]=="" or l[8]=="*"):
#it is ip:*, dont support
print "Error: Dont support ip:* alias settting"
return
else:
if l[7]=="":
l[7]="*"
tr="destination %s:%s"%(l[7],l[8])
if l[9]=="enabled":
res="manual-resume enabled"
else:
res=""
if l[4]!="" and l[5]!="" and l[0]!="icmp":
sr="send \'%s\' recv \'%s\'"%(l[4],l[5])
else:
sr=""
tmsh="tmsh create ltm monitor %s %s interval %s timeout %s %s %s %s\n"%(l[0],l[1],l[2],l[3],sr,tr,res)
print tmsh
f.close()
out=file('tmsh.txt','a')
out.write(tmsh)
out.close
except:
print "Cant open the file,or something wrong in during creating pool. Please turn on debug for detail."
def createpool():
try:
tmsh=""
monitor=""
member=""
mr=""
noratio=1
sda=""
f=file('createpool.txt')
lines=f.readlines()
print lines
l=[]
for line in lines:
print line
#get value of each line
linedata=line.split(",")[1]
#remove "\n"
i=linedata.split("\n")[0]
print i
#create string list for pool
l.append(i)
print l
if l[1]=="":
print "No monitor, will use tcp as default"
monitor="monitor tcp"
print l
else:
#handle monitor if it is more than 1
if l[2]=="":
#no min of monitor setting
monitorstring=l[1].replace("|"," and ")
monitor="monitor %s"%monitorstring
else:
#consider min of monitor
monitorstring=l[1].replace("|"," ")
monitor="monitor min %s of { %s }"%(l[2],monitorstring)
print monitor
print l
if l[3]=="":
sda=""
else:
sda="service-down-action %s"%l[3]
if l[4]=="ratio-member" or l[4]=="ratio-least-connections-member":
noratio=0
if l[4]=="":
l[4]="round-robin"
if l[5]=="":
#no priority group setting
p=""
else:
#has priority, need set min-active-members in tmsh
p="min-active-members %s"%l[5]
if l[6]=="":
print "No members!"
return
else:
#handle members if it is more than 1
member=l[6].split("|")
for m in member:
m1=m.split(":")
if m1[2]=="" or noratio==1:
m1[2]="1"
if m1[3]=="":
m1[3]="0"
if p:
mr=mr + " %s:%s { ratio %s priority-group %s} "%(m1[0],m1[1],m1[2],m1[3])
else:
mr=mr + " %s:%s { ratio %s } "%(m1[0],m1[1],m1[2])
print mr
tmsh="tmsh create ltm pool %s members add {%s} %s load-balancing-mode %s %s %s\n"%(l[0],mr,monitor,l[4],sda,p)
f.close()
out=file('tmsh.txt','a')
out.write(tmsh)
out.close
print tmsh
except:
print "Cant open the file,or something wrong in during creating pool. Please turn on debug for detail."
def createvs():
try:
f=file('createvs.txt')
lines = f.readlines()
print lines
vlan=""
snat=""
l=[]
tmsh=''
for line in lines:
print line
#get value of each line
linedata=line.split(",")[1]
#remove "\n"
i= linedata.split("\n")[0]
print i
#create string list for vs
l.append(i)
print l
if l[3]=="":
#vs_type is blank, so it will be performance l4 or standard vs
if l[13]!="":
#vlan enabled_on has setting
vlan="vlans add { %s } vlans-enabled"%l[13]
if l[14]!="":
#vlan disabled_on has setting
vlan="vlans add { %s } vlans-disabled"%l[14]
if l[15]!="":
#it is snat automap
snat="source-address-translation { type automap } "
if l[16]!="":
#it is snat pool
snat="source-address-translation { type snat pool %s }"%l[16]
tmsh="tmsh create ltm virtual %s destination %s:%s ip-protocol %s profiles add { %s %s %s %s %s %s %s %s} %s %s mirror %s persist replace-all-with { %s %s } pool %s rules { %s %s} %s\n"\
%(l[0],l[1],l[2],l[4],l[5],l[6],l[7],l[8],l[9],l[10],l[11],l[12],vlan,snat,l[17],l[18],l[19],l[20],l[21],l[22],l[23])
elif l[3]=="ipforwarding":
print "it is ip forwarding vs"
return
else:
print "Not support vs type"
return
print tmsh
f.close()
out=file('tmsh.txt','a')
out.write(tmsh)
out.close()
except:
print "Cant open the file,or something wrong in during creating vs. Please turn on debug for detail."
createmonitor()
createpool()
createvs()
Array
文章评论