对Python 网络设备巡检脚本的实例讲解
1、基本信息
我公司之前采用的是人工巡检,但奈何有大量网络设备,往往巡检需要花掉一上午(还是手速快的话),浪费时间浪费生命。
这段时间正好在学 python ,于是乎想(其)要(实)解(就)放(是)双(懒)手。
好了,脚本很长又比较挫,有耐心就看看吧。
需要巡检的设备如下:
设备清单 |
设备型号 |
防火墙 |
华为 e8000e |
|
h3c m9006 |
|
飞塔 fg3950b |
交换机 |
华为 s9306 |
|
h3c s12508 |
|
cisco n7k |
路由器 |
华为 ne40e |
负载 |
radware rd5412 |
|
radware rd6420 |
2、采集数据并写入文件
# 相关设备巡检命令就不介绍了。。。
01dmz-e8000e.py # 华为 e8000e 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime d1=datetime.datetime.now() d3=d1+datetime.timedelta(days=-1) tdy=d3.strftime('%b %d') today=datetime.date.today().strftime('%y%m%d') tt=tdy.split() if int(tt[-1]) < 10: tdy=tdy.replace('0',' ') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) password=str(sys.argv[3]) child=pexpect.spawn('ssh 用户名@%s'%ip) fout=file('/usr/sh/shell/linux/xunjian/'+today+'/01dmz-e8000e.txt','w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect('(?i)e8000e-1>') child.sendline("su") child.expect("(?i)assword:") child.sendline("%s"%password) child.expect("(?i)e8000e-1>") child.sendline("dis device | ex normal") child.expect("(?i)e8000e-1>") child.sendline("dis version") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis temperature") child.expect("(?i)e8000e-1>") child.sendline("dir") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis memory-usage") child.expect("(?i)e8000e-1>") child.sendline("dis hrp state") child.expect("(?i)e8000e-1>") child.sendline("dis firewall session table") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis ip routing-table protocol static") child.expect("(?i)e8000e-1>") child.sendline("dis int brief | in up") child.expect("(?i)e8000e-1>") child.sendline("dis acl 3004") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis acl 3005") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis acl 3006") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis acl 3007") index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send("a") child.expect("(?i)e8000e-1>") child.sendline("dis alarm all") child.expect("(?i)e8000e-1>") child.sendline("dis logbuffer | in %s"%tdy) for i in range(20): index = child.expect(["(?i)---- more ----","(?i)e8000e-1>"]) if ( index == 0 ): child.send(" ") else: child.sendline("q") break
02_03-m9006.py # h3c m9006 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime d1=datetime.datetime.now() d3=d1+datetime.timedelta(days=-1) tdy=d3.strftime('%b') today=datetime.date.today().strftime('%y%m%d') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) password=str(sys.argv[3]) if ip == '判断ip,手动打码': txt='03sm-m9006.txt' else: txt='02dmz-m9006.txt' child=pexpect.spawn('ssh 用户名@%s'%ip) fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect('(?i)m9006>') child.sendline("su") child.expect("(?i)assword:") child.sendline("%s"%password) child.expect("m9006>") child.sendline("dis device | ex normal") child.expect("m9006>") child.sendline("dis version | in uptime") child.expect("m9006>") child.sendline("dis environment") for i in range(10): index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") else: child.sendline("dir | in total") break child.expect("m9006>") child.sendline("dis memory") for i in range(10): index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") else: child.sendline("dis irf link") break child.expect("m9006>") child.sendline("dis redundancy group") child.expect("---- more ----") child.send('a') child.expect("m9006>") child.sendline("dis session statistics summary") child.expect("m9006>") child.sendline("dis ip routing-table protocol static") child.expect("m9006>") child.sendline("dis int brief | in up") for i in range(10): index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") else: break if ip == '判断ip': child.sendline("dis acl 3001") for i in range(10): index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") else: child.sendline("dis acl 3002") child.expect("m9006>") child.sendline("dis alarm") break else: child.sendline("dis object-policy ip") for i in range(20): index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") else: child.sendline("dis alarm") break child.expect("m9006>") child.sendline("dis logbuffer reverse | in %s"%tdy) index = child.expect(["(?i)---- more ----","m9006>"]) 这边其实只要一个 for 循环就可以了,不高兴改了 if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send("a") index = child.expect(["(?i)---- more ----","m9006>"]) if ( index == 0 ): child.send("a") else: child.sendline('') child.expect("m9006>") child.sendline("q")
04in-fg3950b.py # 飞塔 fg3950b 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime today=datetime.date.today().strftime('%y%m%d') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) child=pexpect.spawn('ssh 用户@%s'%ip) fout=file('/usr/sh/shell/linux/xunjian/'+today+'/04in-fg3950b.txt','w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect('w-in-fg3950-1 #') child.sendline("get sys performance status") for i in range(5): index = child.expect(["(?i)--more--","w-in-fg3950-1 #"]) if ( index == 0 ): child.send(" ") else: child.sendline("diagnose sys session stat") break child.expect('w-in-fg3950-1 #') child.sendline("get route info routing-table static") child.expect('--more--') child.send(' ') child.expect('w-in-fg3950-1 #') child.sendline("exit")
05_06dmz-s9306.py # 华为 s9306 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime d1=datetime.datetime.now() d3=d1+datetime.timedelta(days=-1) tdy=d3.strftime('%b %d') today=datetime.date.today().strftime('%y%m%d') tt=tdy.split() if int(tt[-1]) < 10: tdy=tdy.replace('0',' ') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) password=str(sys.argv[3]) child=pexpect.spawn('ssh 用户名@%s'%ip) if ip == '判断ip,不止一个该类型设备': txt='05dmz-s9306-1.txt' else: txt='06dmz-s9306-2.txt' fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect('<jsyd-wx-12580-dmz-9306') child.sendline("su") child.expect("(?i)assword:") child.sendline("%s"%password) child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis device") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis version | in quidway") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dir") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis cpu-usage") index = child.expect(["(?i)---- more ----","<jsyd-wx-12580-dmz-9306(?i)"]) if ( index == 0 ): child.send("a") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis memory-usage") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis int brief | in up") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis vrrp brief") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis mac-address total-number") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis arp statistics all") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis ip routing-table protocol static") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis temperature all") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis alarm all") child.expect("<jsyd-wx-12580-dmz-9306") child.sendline("dis logbuffer | in %s"%tdy) for i in range(50): index = child.expect(["(?i)---- more ----","<jsyd-wx-12580-dmz-9306(?i)"]) if ( index == 0 ): child.send(" ") else: child.sendline("q") break
07_08-s12508.py # h3c s12508 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime d1=datetime.datetime.now() d3=d1+datetime.timedelta(days=-1) tdy=d3.strftime('%b %d') today=datetime.date.today().strftime('%y%m%d') tt=tdy.split() if int(tt[-1]) < 10: tdy=tdy.replace('0',' ') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) password=str(sys.argv[3]) child=pexpect.spawn('ssh 用户名@%s'%ip) ip=['ip1','ip2'] if ip == ip[0]: txt='07dmz-s12508.txt' name='<w-dmz-g1g2-12508>' else: txt='08in-s12508.txt' name='<w-in-g7g8-12508>' fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect("%s"%name) child.sendline("su") child.expect("(?i)assword:") child.sendline("%s"%password) child.expect("%s"%name) child.sendline("dis device | ex normal") child.expect("%s"%name) child.sendline("dis version | in h3c s12508") child.expect("%s"%name) child.sendline("dir") child.expect("%s"%name) child.sendline("dis memory") child.expect("%s"%name) child.sendline("dis irf link") for i in range(10): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis int brief | in up") break for i in range(20): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis mac-address count") break child.expect("%s"%name) child.sendline("dis arp all count") if ip == "手动打码": child.expect("%s"%name) child.sendline("dis ip routing-table vpn-instance dmz protocol static") for i in range(20): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis ip routing-table vpn-instance inside protocol static") for i in range(20): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline(" ") break break child.expect("%s"%name) child.sendline("dis environment") for i in range(50): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis alarm") break child.expect("%s"%name) child.sendline("dis logbuffer | in %s"%tdy) for i in range(50): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("q") sys.exit()
09_10sm-n7k.py # cisco n7k 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime today=datetime.date.today().strftime('%y%m%d') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) child=pexpect.spawn('telnet %s'%ip) if ip == '是个ip': txt='09sm-n7k-1.txt' name='w-sm-n7k-1#' else: txt='10sm-n7k-2.txt' name='w-sm-n7k-2#' fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w') child.logfile = fout child.expect('login:') child.sendline("发送用户名") child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect("%s"%name) child.sendline("sh hardware | in ok") child.expect("%s"%name) child.sendline("sh version | in uptime") child.expect("%s"%name) child.sendline("dir") child.expect("%s"%name) child.sendline("sh int brief | in up") for i in range(10): index = child.expect(["(?i)more--","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("sh hsrp brief") break child.expect("%s"%name) child.sendline("sh mac address-table count") child.expect("%s"%name) child.sendline("sh ip arp | in number") child.expect("%s"%name) child.sendline("sh ip route static summary") child.expect("%s"%name) child.sendline("sh environment temperature") for i in range(10): index = child.expect(["(?i)more--","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("sh vpc") break for i in range(10): index = child.expect(["(?i)more--","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("sh logging last 30") break for i in range(10): index = child.expect(["(?i)more--","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("exit") sys.exit()
11_12dmz-ne40e.py # 华为 ne40e 交互脚本
#!/usr/bin/env python import pexpect import sys import datetime d1=datetime.datetime.now() d3=d1+datetime.timedelta(days=-1) tdy=d3.strftime('%b %d') today=datetime.date.today().strftime('%y%m%d') tt=tdy.split() if int(tt[-1]) < 10: tdy=tdy.replace('0',' ') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) child=pexpect.spawn('ssh 用户名@%s'%ip) ip=['ip1','ip2'] if ip == ip[0]: txt='11dmz-ne40e-1.txt' name='<w-sm-e11-ne40e-01>' elif ip == ip[1]: txt='12dmz-ne40e-2.txt' name='<w-sm-e12-ne40e-02>' else: print "ip input error!" sys.exit() fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) child.expect("(?i)n]:") child.sendline("n") child.expect("%s"%name) child.sendline("dis device | ex normal") child.expect("%s"%name) child.sendline("dis version | in ne40e-x8 uptime") child.expect("%s"%name) child.sendline("dir") index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis memory-usage") else: child.sendline("dis memory-usage") child.expect("%s"%name) child.sendline("dis int brief | in up") child.expect("%s"%name) child.sendline("dis ip routing-table protocol static") child.expect("%s"%name) child.sendline("dis temperature") for i in range(40): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("dis alarm all") break child.expect("%s"%name) child.sendline("dis logbuffer | in %s"%tdy) for i in range(30): index = child.expect(["(?i)---- more ----","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("q") break
13_16-rd5412_6420.py # radware rd5412 交互脚本,只要掌握一个,其他都是套路
#!/usr/bin/env python import pexpect import sys import datetime d1=datetime.datetime.now() d3=d1+datetime.timedelta(days=-1) tdy=d3.strftime('%b %d') today=datetime.date.today().strftime('%y%m%d') tt=tdy.split() if int(tt[-1]) < 10: tdy=tdy.replace('0',' ') ip=str(sys.argv[1]) passwd=str(sys.argv[2]) child=pexpect.spawn('ssh 还是用户名@%s'%ip) ip=['ip1','ip2','ip3','ip4'] if ip == ip[0]: txt='13dmz-rd5412-1.txt' elif ip == ip[1]: txt='14dmz-rd5412-2.txt' elif ip == ip[2]: txt='15sm-rd6420-1.txt' elif ip == ip[3]: txt='16sm-rd6420-2.txt' else: print "ip input error!" sys.exit() name='standalone adc(?i)' fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w') child.logfile = fout child.expect('(?i)ssword:') child.sendline("%s"%passwd) if ip == ip[3]: child.expect("(?i)y]:") child.sendline("y") child.expect("%s"%name) child.sendline("/i/sys/general") child.expect("%s"%name) child.sendline("/i/sys/ps") child.expect("%s"%name) child.sendline("/i/sys/fan") child.expect("%s"%name) child.sendline("/i/link") index = child.expect(["press q to quit, any other key to continue","%s"%name]) if ( index == 0 ): child.send(" ") index = child.expect(["press q to quit, any other key to continue","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("/i/sys/capacity slb") else: child.sendline("/i/sys/capacity slb") index = child.expect(["press q to quit, any other key to continue","%s"%name]) if ( index == 0 ): child.send(" ") index = child.expect(["press q to quit, any other key to continue","%s"%name]) if ( index == 0 ): child.send(" ") index = child.expect(["press q to quit, any other key to continue","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("/stats/mp/cpu") else: child.sendline("/stats/mp/cpu") else: child.sendline("/stats/mp/cpu") child.expect("%s"%name) child.sendline("/stats/mp/mem") child.expect("%s"%name) child.sendline("/i/sys/temp") child.expect("%s"%name) child.sendline("/i/l3/vrrp") child.expect("%s"%name) child.sendline("/i/sys/log") for i in range(50): index = child.expect(["press q to quit, any other key to continue","%s"%name]) if ( index == 0 ): child.send(" ") else: child.sendline("exit") break #index = child.expect(["(?i)n]:","%s"%name]) #if ( index == 0 ): if ip == ip[3]: child.expect("(?i)n]:") child.sendline("y") #else: # pass child.expect("(?i)n]:") child.sendline("n")
3、编写邮件脚本
mail.py
#!/usr/bin/python # -*- coding: utf-8 -*- from email.header import header import smtplib import email.mimemultipart# import mimemultipart import email.mimetext# import mimetext import email.mimebase# import mimebase import os.path import sys import mimetypes import email.mimeimage# import mimeimage import datetime tday=datetime.date.today().strftime('%y/%m/%d') #命令 mail.py <1:发送方(回复地址)10000@qq.com> <2:发送地址,多个以;隔开> <3:发送文件> from = "%s<*@139.com>" % header("xxx","utf-8") replyto=sys.argv[1] to = sys.argv[2] file_name = sys.argv[3]#附件名 file_name1 = sys.argv[4] server = smtplib.smtp("smtp.139.com",25) server.login("user@139.com","password") #仅smtp服务器需要验证时 # 构造mimemultipart对象做为根容器 main_msg = email.mimemultipart.mimemultipart() # 构造mimetext对象做为邮件显示内容并附加到根容器 text_msg = email.mimetext.mimetext("message内容正文",_charset="utf-8") main_msg.attach(text_msg) # 构造mimebase对象做为文件附件内容并附加到根容器 ctype,encoding = mimetypes.guess_type(file_name) if ctype is none or encoding is not none: ctype='application/octet-stream' maintype,subtype = ctype.split('/',1) file_msg=email.mimeimage.mimeimage(open(file_name,'rb').read(),subtype) file_msg1=email.mimeimage.mimeimage(open(file_name1,'rb').read(),subtype) ## 设置附件头 basename = os.path.basename(file_name) file_msg.add_header('content-disposition','attachment', filename = basename)#修改邮件头 main_msg.attach(file_msg) basename1 = os.path.basename(file_name1) file_msg1.add_header('content-disposition','attachment', filename = basename1)#修改邮件头 main_msg.attach(file_msg1) # 设置根容器属性 main_msg['from'] = from main_msg['reply-to'] = replyto #main_msg['to'] = to main_msg['subject'] = u"[每日巡检] %s邮件标题" %tday main_msg['date'] = email.utils.formatdate() #main_msg['bcc'] = to # 得到格式化后的完整文本 fulltext = main_msg.as_string( ) # 用smtp发送邮件 try: server.sendmail(from, to.split(';'), fulltext) finally: server.quit()
mail_excel_error.py # 报错通知邮件:excel不存在
#!/usr/bin/env python # -*- coding:utf-8 -*- #import sys #reload(sys) import smtplib,string,datetime,xlrd tday=datetime.date.today().strftime('%y/%m/%d') host="smtp.139.com" subject="[每日巡检] %s哈哈哈"%tday to="*.com" from="*@139.com" text="excel no cunzai" body=string.join(( "from: %s" % from, "to: %s" % to, "subject: %s" % subject, "", text ), "\r\n") server=smtplib.smtp() server.connect(host,"25") server.starttls() server.login(from,"密码") server.sendmail(from,[to],body) server.quit()
mail_zip_error.py # 邮件告警:巡检历史记录压缩文档不存在
#!/usr/bin/env python # -*- coding:utf-8 -*- #import sys #reload(sys) import smtplib,string,datetime,xlrd tday=datetime.date.today().strftime('%y/%m/%d') host="smtp.139.com" subject="[每日巡检] %s嘻嘻嘻" %tday to="asdas.com" from="***@139.com" text="zip no cunzai" body=string.join(( "from: %s" % from, "to: %s" % to, "subject: %s" % subject, "", text ), "\r\n") server=smtplib.smtp() server.connect(host,"25") server.starttls() server.login(from,"password") server.sendmail(from,[to],body) server.quit()
4、数据处理
wangluo_xunjian.py # 根据巡检内容制表
#!/usr/bin/env python # -*- coding:utf-8 -*- #import sys #reload(sys) import xlsxwriter,os,datetime def cmd(sname,fname): # 该函数判断巡检命令行所在输出文件位置 i=1 f=open(fname) line_hang_list=[] for line in f: if line.find(sname) == 0: line_hang_list.append(i) i+=1 f.close return line_hang_list def cmd1(sname,fname): # 同上 i=1 f=open(fname) line_hang_list=[] for line in f: if sname in line: line_hang_list.append(i) i+=1 f.close return line_hang_list tday=datetime.date.today().strftime('%y.%m.%d') today=datetime.date.today().strftime('%y%m%d') workbook=xlsxwriter.workbook(u"/usr/sh/shell/linux/xunjian/%s/网络设备巡检-%s.xlsx"%(today,tday),{'strings_to_numbers':true}) file_01='/usr/sh/shell/linux/xunjian/'+str(today)+'/01dmz-e8000e.txt' file_02_1='/usr/sh/shell/linux/xunjian/'+str(today)+'/03sm-m9006.txt' file_02_2='/usr/sh/shell/linux/xunjian/'+str(today)+'/02dmz-m9006.txt' file_03='/usr/sh/shell/linux/xunjian/'+str(today)+'/04in-fg3950b.txt' # 新建 sheet worksheet0=workbook.add_worksheet(u"防火墙") worksheet1=workbook.add_worksheet(u"交换机") worksheet2=workbook.add_worksheet(u"路由器") worksheet3=workbook.add_worksheet(u"负载") # 设置单元格格式(居中,颜色,边框,合并,列宽,列隐藏等) colour='#660099' format_title=workbook.add_format() format_title.set_border(1) format_title.set_bottom(2) format_title.set_bold(1) format_title.set_font_color('white') format_title.set_bg_color(colour) format_title.set_align('center') format_title.set_valign('vcenter') format_nr=workbook.add_format() format_nr.set_border(1) format_nr.set_bottom(2) format_nr.set_align('center') format_nr.set_valign('vcenter') format_nrr=workbook.add_format() format_nrr.set_border(1) format_nrr.set_valign('vcenter') format_nrr.set_text_wrap() format_bottom=workbook.add_format() format_bottom.set_border(1) format_bottom.set_bottom(2) format_bottom.set_valign('vcenter') format_bottom.set_text_wrap() format_rt=workbook.add_format() format_rt.set_border(1) format_rt.set_right(2) format_rt.set_valign('left') format_rt.set_text_wrap() format_right=workbook.add_format() format_right.set_border(1) format_right.set_right(2) format_right.set_bottom(2) format_right.set_valign('vcenter') format_right.set_text_wrap() format_red=workbook.add_format() format_red.set_font_color('red') format_red.set_border(1) format_red.set_right(2) format_red.set_valign('vcenter') format_rd=workbook.add_format() format_rd.set_font_color('red') format_rd.set_border(1) format_rd.set_right(2) format_rd.set_bottom(2) format_rd.set_valign('vcenter') # 填写相关固定单元格内容 firewall_title=[u'设备名称',u'设备类型',u'ip地址',u'检查项',u'命令',u'说明',u'检查结果'] firewall_jc=[u'设备状态',u'运行时长',u'运行温度',u'flash状态',u'cpu使用率',u'内存使用率',u'冗余组状态',u'session数目',u'路由条数',u'端口状态',u'策略条数',u'告警',u'日志',u'板卡状态',u'运行时长',u'运行温度',u'flash状态',u'cpu使用率',u'内存使用率',u'堆叠状态',u'冗余组状态',u'session数目',u'路由条数',u'端口状态',u'策略条数',u'告警',u'日志',u'板卡状态',u'运行时长',u'运行温度',u'flash状态',u'cpu使用率',u'内存使用率',u'堆叠状态',u'冗余组状态',u'session数目',u'路由条数',u'端口状态',u'策略条数',u'告警',u'日志',u'运行时长',u'cpu使用率 (bomc看)',u'内存使用率',u'网络流量',u'session数目',u'新建session速率',u'接口状态',u'冗余组状态',u'路由条数',u'日志',u'策略条数',u'告警'] firewall_cmd=['dis device | ex normal','dis version','dis temperature','dir',u'在bomc看','dis memory-usage','dis hrp state','dis firewall session table','dis ip routing-table protocol static','dis int brief (dis logbuffer | in down)','dis acl 3004 (3004-3007)','dis alarm all','dis logbuffer | in [data]','dis device | ex normal','dis version | in uptime','dis environment','dir | in total',u'在bomc看','dis memory','dis irf link','dis redundancy group','dis session statistics summary','dis ip routing-table protocol static','dis int brief (dis logbuffer | in down)','dis object-policy ip','dis alarm','dis logbuffer | in [month]','dis device | ex normal','dis version | in uptime','dis environment','dir | in total',u'在bomc看','dis memory','dis irf link','dis redundancy group','dis session statistics summary','dis ip routing-table protocol static','dis int brief (dis logbuffer | in down)','dis acl all','dis alarm','dis logbuffer | in [month]','get sys performance status','','','','diagnose sys session stat','',u'网页登录查看',u'网页登录查看','get route info routing table static',u'网页登录查看',u'网页登录查看',u'网页登录查看'] firewall_sm=[u'填写status是abnormal的项',u'填写已运行天数,小时数',u'填写温度范围',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写本机和邻居的角色,看有无发生主备切换',u'填写链接总数',u'填写路由数目',u'填写状态为up的端口数目',u'填写策略数目',u'填写告警内容,无告警则填写“无告警”',u'填写异常日志,无异常则填写“正常”',u'填写state为absent,且type不为none的项',u'填写已运行天数,小时数',u'填写温度范围',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率范围',u'填写status不是up的项',u'填写status,看有无发生主备切换',u'填写链接总数',u'填写路由数目',u'填写状态为up的端口数目',u'填写策略数目(所有rule的总数)',u'填写告警内容,无告警则填写“无告警”',u'填写异常日志,无异常则填写“正常”',u'填写state为absent,且type不为none的项',u'填写已运行天数,小时数',u'填写温度范围',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率范围',u'填写status不是up的项',u'填写status,看有无发生主备切换',u'填写链接总数',u'填写路由数目',u'填写状态为up的端口数目',u'填写策略数目(acl 3001+3002的总条目数)',u'填写告警内容,无告警则填写“无告警”',u'填写异常日志,无异常则填写“正常”',u'填写已运行天数,小时数',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写30分钟内平均带宽占用(kbps)',u'填写session_count值',u'填写setup_rate值',u'填写启用状态的接口数目',u'填写冗余状态',u'填写路由数目',u'填写异常日志,无异常则填写“正常”',u'填写策略数目',u'填写告警内容,无告警则填写“无告警”'] switch_title=[u'设备名称',u'设备类型',u'ip地址',u'检查项',u'命令',u'说明',u'检查结果'] switch_jc=[u'设备状态',u'运行时长',u'flash状态',u'cpu使用率',u'内存使用率',u'接口状态',u'vrrp状态',u'mac地址条目数',u'arp条目数',u'路由条目数',u'运行温度',u'告警',u'日志'] switch_jc1=[u'设备状态',u'运行时长',u'flash状态',u'cpu使用率',u'内存使用率',u'堆叠状态',u'接口状态',u'mac地址条目数',u'arp条目数',u'路由条目数',u'运行温度',u'告警',u'日志'] switch_jc2=[u'设备状态',u'运行时长',u'flash状态',u'cpu使用率',u'内存使用率',u'堆叠状态',u'接口状态',u'mac地址条目数',u'arp条目数',u'运行温度',u'告警',u'日志'] switch_cmd=['dis device','dis version | in quidway','dir','dis cpu-usage','dis memory-usage','dis int brief (dis logbuffer | in if_state)','dis vrrp brief','dis mac-address total-number','dis arp statistics all','dis ip routing-table protocol static','dis temperature all','dis alarm all','dis logbuffer | in [date]'] switch_cmmd=['dis device | ex normal','dis version | in h3c s12508','dir',u'在bomc看','dis memory','dis irf link','dis int brief (dis logbuffer reverse | in updown)','dis mac-address count','dis arp all count','dis ip routing-table vpn-instance dmz/inside protocol static','dis environment','dis alarm','dis logbuffer reverse | in [date]'] switch_cmd1=['dis device | ex normal','dis version | in h3c s12508','dir',u'在bomc看','dis memory','dis int brief (dis logbuffer reverse | in updown)','dis vrrp','dis mac-address count','dis arp all count','dis ip routing-table protocol static','dis environment','dis alarm','dis logbuffer reverse | in [date]'] switch_cmd2=['dis device | ex normal','dis version | in h3c s12508','dir',u'在bomc看','dis memory','dis irf link','dis int brief (dis logbuffer reverse | in updown)','dis mac-address count','dis arp all count','dis environment','dis alarm','dis logbuffer reverse | in [date]'] switch_sm=[u'查看电源、风扇以及板卡状态,填写异常项',u'填写已运行天数,小时数',u'填写total和free的flash大小',u'填写cpu使用率',u'填写内存使用率',u'填写状态为up的端口数目',u'查看vrrp主备状态',u'填写总的mac-address条目数',u'填写动态和静态arp条目数',u'填写路由数目',u'填写温度范围',u'填写告警内容,无告警则填写“无告警”',u'填写异常日志,无异常则填写“正常”'] switch_ssm=[u'填写状态异常的项',u'填写已运行天数,小时数',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写status不是up的项',u'填写状态为up的端口数目',u'填写总的mac-address条目数',u'填写总的arp条目数',u'填写路由数目(dmz和inside路由总和)',u'填写温度范围',u'填写设备告警,无则填写“无告警”',u'填写异常日志,无异常则填写“正常”'] switch_sm1=[u'填写状态异常的项',u'填写已运行天数,小时数',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写状态为up的端口数目',u'查看vrrp主备状态',u'填写总的mac-address条目数',u'填写总的arp条目数',u'填写路由数目',u'填写温度范围',u'填写设备告警,无则填写“无告警”',u'填写异常日志,无异常则填写“正>常”'] switch_sm2=[u'填写状态异常的项',u'填写已运行天数,小时数',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写status不是up的项',u'填写状态为up的端口数目',u'填写总的mac-address条目数',u'填写总的arp条目数',u'填写温度范围',u'填写设备告警,无则填写“无告警”',u'填写异常日志,无异常则填写“正>常”'] route_title=[u'设备名称',u'设备类型',u'ip地址',u'检查项',u'命令',u'说明',u'检查结果'] route_jc=[u'设备状态',u'运行时长',u'flash状态',u'cpu使用率',u'内存使用率',u'接口状态',u'路由条数',u'运行温度',u'告警',u'日志'] route_cmd=['dis device | ex normal','dis version | in ne40e-x8 uptime','dir',u'在bomc看','dis memory-usage','dis int brief (dis logbuffer | in if_state)','dis ip routing-table protocol static','dis temperature','dis alarm all','dis logbuffer | in [date]'] route_sm=[u'填写status不是“normal”的项',u'填写已运行天数,小时数',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写状态为up的端口数目',u'填写路由数目',u'填写温度范围',u'填写告警内容,无告警则填写“无告警”',u'填写异常日志,无异常则填写“正常”'] fuzai_title=[u'设备名称',u'设备类型',u'ip地址',u'检查项',u'命令',u'说明',u'检查结果'] fuzai_jc=[u'运行时长',u'电源状态',u'风扇状态',u'端口状态',u'各表项数值',u'cpu使用率',u'内存使用率',u'硬件温度',u'冗余状态',u'日志'] fuzai_cmd=['/i/sys/general','/i/sys/ps','/i/sys/fan','/i/link','/i/sys/capacity slb','/stats/mp/cpu','/stats/mp/mem','/i/sys/temp','/i/l3/vrrp','/i/sys/log'] fuzai_sm=[u'填写已运行天数,小时数',u'填写电源状态',u'填写风扇状态',u'填写link处于up状态的端口数',u'填写real servers/server groups/virtual servers/filters当前值',u'填写cpu使用率',u'填写内存使用率',u'填写温度',u'填写vrrp状态,"master"或者"backup"',u'填写异常日志,无异常则填写“正常”'] newsmswitch_title=[u'设备状态',u'运行时长',u'flash状态',u'cpu使用率',u'内存使用率',u'接口状态',u'hsrp状态',u'mac地址条目数',u'arp条目数',u'路由条目数',u'运行温度',u'vpc状态',u'日志'] newsmswitch_cmd=['sh hardware | in ok','sh version | in uptime','dir',u'在bomc看',u'在bomc看','sh int brief (sh int brief | in up)','sh hsrp brief','sh mac address-table count','sh ip arp | in number','sh ip route static summary','sh environment temperature','sh vpc','sh logging last 30'] newsmswitch_sm=[u'填写状态为ok的模块数',u'填写已运行天数,小时数',u'填写total和free的flash大小',u'填写cpu使用率峰值和平均值',u'填写内存使用率',u'填写状态为up的端口数目',u'查看hsrp状态(active local则正常)',u'填写总的mac-address条目数',u'填写总的arp条目数',u'填写路由数目',u'填写curtemp范围',u'查看vpc状态是否正常',u'查看日志,无异常则填写"正常"'] # ip 我就隐藏掉啦,虽然也没什么意思 host={"dmz-e8000e":[u'防火墙',"ip1"],"dmz-m9006":[u'防火墙','ip2'],"sm-m9006":[u'防火墙','ip3'],"in-fg3950b":[u'防火墙',"ip4"],"dmz-s9306-1":[u'交换机',"ip5"],"dmz-s9306-2":[u'交换机',"ip6"],"dmz-s12508":[u'交换机',"ip7"],"in-s12508":[u'交换机',"ip8"],"sm-s12508-1":[u'交换机',"ip9"],"sm-s12508-2":[u'交换机',"ip10"],"dmz-ne40e-1":[u'路由器',"ip11"],"dmz-ne40e-2":[u'路由器',"ip12"],"dmz-rd5412-1":[u'负载',"ip13"],"dmz-rd5412-2":[u'负载',"ip14"],"sm-rd6420-1":[u'负载',"ip15"],"sm-rd6420-2":[u'负载',"ip16"],"sm-n7k-1":[u"交换机","ip17"],"sm-n7k-2":[u"交换机","ip18"]} # 开始填写固定单元格内容 worksheet0.write_row('a1',firewall_title,format_title) worksheet0.merge_range('a2:a14','dmz-e8000e',format_nr) worksheet0.merge_range('b2:b14',host["dmz-e8000e"][0],format_nr) worksheet0.merge_range('c2:c14',host["dmz-e8000e"][1],format_nr) worksheet0.merge_range('a15:a28','dmz-m9006',format_nr) worksheet0.merge_range('b15:b28',host["dmz-m9006"][0],format_nr) worksheet0.merge_range('c15:c28',host["dmz-m9006"][1],format_nr) worksheet0.merge_range('a29:a42','sm-m9006',format_nr) worksheet0.merge_range('b29:b42',host["sm-m9006"][0],format_nr) worksheet0.merge_range('c29:c42',host["sm-m9006"][1],format_nr) worksheet0.merge_range('a43:a54','in-fg3950b',format_nr) worksheet0.merge_range('b43:b54',host["in-fg3950b"][0],format_nr) worksheet0.merge_range('c43:c54',host["in-fg3950b"][1],format_nr) worksheet0.merge_range('e43:e46','get sys performance status',format_nrr) worksheet0.merge_range('e47:e48','diagnose sys session stat',format_nrr) worksheet0.write_column('d2',firewall_jc,format_nrr) worksheet0.write_column('e2',firewall_cmd,format_nrr) worksheet0.write_column('f2',firewall_sm,format_nrr) worksheet0.write('d14',u'日志',format_bottom) worksheet0.write('e14','dis logbuffer | in [data]',format_bottom) worksheet0.write('f14',u'填写异常日志,无异常则填写“正常”',format_bottom) worksheet0.write('d28',u'日志',format_bottom) worksheet0.write('e28','dis logbuffer reverse | in [month]',format_bottom) worksheet0.write('f28',u'填写异常日志,无异常则填写“正常”',format_bottom) worksheet0.write('d42',u'日志',format_bottom) worksheet0.write('e42','dis logbuffer reverse | in [month]',format_bottom) worksheet0.write('f42',u'填写异常日志,无异常则填写“正常”',format_bottom) worksheet0.write('d54',u'告警',format_bottom) worksheet0.write('e54',u'网页登录查看',format_bottom) worksheet0.write('f54',u'填写告警内容,无告警则填写“无告警”',format_bottom) worksheet0.set_row(0,30) worksheet0.set_column('b:b',15,none,{'hidden':1}) worksheet0.set_column('e:f',40,none,{'hidden':1}) worksheet0.set_column('a:a',15) worksheet0.set_column('c:d',15) worksheet0.set_column('g:g',40) worksheet1.write_row('a1',switch_title,format_title) worksheet1.merge_range('a2:a14','dmz-s9306-1',format_nr) worksheet1.merge_range('b2:b14',host["dmz-s9306-1"][0],format_nr) worksheet1.merge_range('c2:c14',host["dmz-s9306-1"][1],format_nr) worksheet1.merge_range('a15:a27','dmz-s9306-2',format_nr) worksheet1.merge_range('b15:b27',host["dmz-s9306-2"][0],format_nr) worksheet1.merge_range('c15:c27',host["dmz-s9306-2"][1],format_nr) worksheet1.merge_range('a28:a40','dmz-s12508',format_nr) worksheet1.merge_range('b28:b40',host["dmz-s12508"][0],format_nr) worksheet1.merge_range('c28:c40',host["dmz-s12508"][1],format_nr) worksheet1.merge_range('a41:a52','in-s12508',format_nr) worksheet1.merge_range('b41:b52',host["in-s12508"][0],format_nr) worksheet1.merge_range('c41:c52',host["in-s12508"][1],format_nr) worksheet1.merge_range('a53:a65','sm-n7k-1',format_nr) worksheet1.merge_range('b53:b65',host["sm-n7k-1"][0],format_nr) worksheet1.merge_range('c53:c65',host["sm-n7k-1"][1],format_nr) worksheet1.merge_range('a66:a78','sm-n7k-2',format_nr) worksheet1.merge_range('b66:b78',host["sm-n7k-2"][0],format_nr) worksheet1.merge_range('c66:c78',host["sm-n7k-2"][1],format_nr) worksheet1.write_column('d2',switch_jc,format_nrr) worksheet1.write_column('d15',switch_jc,format_nrr) worksheet1.write_column('d28',switch_jc1,format_nrr) worksheet1.write_column('d41',switch_jc2,format_nrr) worksheet1.write_column('d53',newsmswitch_title,format_nrr) worksheet1.write_column('d66',newsmswitch_title,format_nrr) worksheet1.write_column('e2',switch_cmd,format_nrr) worksheet1.write_column('e15',switch_cmd,format_nrr) worksheet1.write_column('e28',switch_cmmd,format_nrr) worksheet1.write_column('e41',switch_cmd2,format_nrr) worksheet1.write_column('e53',newsmswitch_cmd,format_nrr) worksheet1.write_column('e66',newsmswitch_cmd,format_nrr) worksheet1.write_column('f2',switch_sm,format_nrr) worksheet1.write_column('f15',switch_sm,format_nrr) worksheet1.write_column('f28',switch_ssm,format_nrr) worksheet1.write_column('f41',switch_sm2,format_nrr) worksheet1.write_column('f53',newsmswitch_sm,format_nrr) worksheet1.write_column('f66',newsmswitch_sm,format_nrr) worksheet1.set_row(0,30) worksheet1.set_column('b:b',15,none,{'hidden':1}) worksheet1.set_column('e:f',40,none,{'hidden':1}) worksheet1.set_column('a:a',15) worksheet1.set_column('c:d',15) worksheet1.set_column('g:g',40) worksheet2.write_row('a1',route_title,format_title) worksheet2.merge_range('a2:a11','dmz-ne40e-1',format_nr) worksheet2.merge_range('b2:b11',host["dmz-ne40e-1"][0],format_nr) worksheet2.merge_range('c2:c11',host["dmz-ne40e-1"][1],format_nr) worksheet2.merge_range('a12:a21','dmz-ne40e-2',format_nr) worksheet2.merge_range('b12:b21',host["dmz-ne40e-2"][0],format_nr) worksheet2.merge_range('c12:c21',host["dmz-ne40e-2"][1],format_nr) worksheet2.write_column('d2',route_jc,format_nrr) worksheet2.write_column('d12',route_jc,format_nrr) worksheet2.write_column('e2',route_cmd,format_nrr) worksheet2.write_column('e12',route_cmd,format_nrr) worksheet2.write_column('f2',route_sm,format_nrr) worksheet2.write_column('f12',route_sm,format_nrr) worksheet2.set_row(0,30) worksheet2.set_column('b:b',15,none,{'hidden':1}) worksheet2.set_column('e:f',40,none,{'hidden':1}) worksheet2.set_column('a:a',15) worksheet2.set_column('c:d',15) worksheet2.set_column('g:g',40) worksheet3.write_row('a1',fuzai_title,format_title) worksheet3.merge_range('a2:a11','dmz-rd5412-1',format_nr) worksheet3.merge_range('b2:b11',host["dmz-rd5412-1"][0],format_nr) worksheet3.merge_range('c2:c11',host["dmz-rd5412-1"][1],format_nr) worksheet3.merge_range('a12:a21','dmz-rd5412-2',format_nr) worksheet3.merge_range('b12:b21',host["dmz-rd5412-2"][0],format_nr) worksheet3.merge_range('c12:c21',host["dmz-rd5412-2"][1],format_nr) worksheet3.merge_range('a22:a31','sm-rd6420-1',format_nr) worksheet3.merge_range('b22:b31',host["sm-rd6420-1"][0],format_nr) worksheet3.merge_range('c22:c31',host["sm-rd6420-1"][1],format_nr) worksheet3.merge_range('a32:a41','sm-rd6420-2',format_nr) worksheet3.merge_range('b32:b41',host["sm-rd6420-2"][0],format_nr) worksheet3.merge_range('c32:c41',host["sm-rd6420-2"][1],format_nr) worksheet3.write_column('d2',fuzai_jc,format_nrr) worksheet3.write_column('d12',fuzai_jc,format_nrr) worksheet3.write_column('d22',fuzai_jc,format_nrr) worksheet3.write_column('d32',fuzai_jc,format_nrr) worksheet3.write_column('e2',fuzai_cmd,format_nrr) worksheet3.write_column('e12',fuzai_cmd,format_nrr) worksheet3.write_column('e22',fuzai_cmd,format_nrr) worksheet3.write_column('e32',fuzai_cmd,format_nrr) worksheet3.write_column('f2',fuzai_sm,format_nrr) worksheet3.write_column('f12',fuzai_sm,format_nrr) worksheet3.write_column('f22',fuzai_sm,format_nrr) worksheet3.write_column('f32',fuzai_sm,format_nrr) worksheet3.set_row(0,30) worksheet3.set_column('b:b',15,none,{'hidden':1}) worksheet3.set_column('e:f',40,none,{'hidden':1}) worksheet3.set_column('a:a',15) worksheet3.set_column('c:d',15) worksheet3.set_column('g:g',40) jcx=[u'日志','dis logbuffer | in [date]',u'填写异常日志,无异常则填写“正常”'] jcx1=[u'日志','dis logbuffer reverse | in [date]',u'填写异常日志,无异常则填写“正常”'] jcx2=[u'日志','sh logging last 30',u'查看日志,无异常则填写“正常”'] fuzai_jcx=[u'日志','/i/sys/log',u'填写异常日志,无异常则填写“正常”'] worksheet1.write_row('d14',jcx,format_bottom) worksheet1.write_row('d27',jcx,format_bottom) worksheet1.write_row('d40',jcx,format_bottom) worksheet1.write_row('d52',jcx1,format_bottom) worksheet1.write_row('d65',jcx2,format_bottom) worksheet1.write_row('d78',jcx2,format_bottom) worksheet2.write_row('d11',jcx,format_bottom) worksheet2.write_row('d21',jcx,format_bottom) worksheet3.write_row('d11',fuzai_jcx,format_bottom) worksheet3.write_row('d21',fuzai_jcx,format_bottom) worksheet3.write_row('d31',fuzai_jcx,format_bottom) worksheet3.write_row('d41',fuzai_jcx,format_bottom) # 这边开始根据之前与设备交互所得到的数据文件内容来填格子了。 #------------------------------------------------ #----------firewall_ip1---------------- #------------------------------------------------ try: hang=1 temp=[] hang_list=cmd("hrp_m<jsyd-wx-12580-dmz-e8000e-1>",file_01) rules=[] g=0 with open(file_01) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[1]) and hang < int(hang_list[2]): if int(hang_list[2])-int(hang_list[1]) == 6: worksheet0.write('g2',u'正常',format_rt) else: worksheet0.write('g2','error',format_red) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if num[0] == 'huawei': worksheet0.write('g3',u'%s天,%s小时'%(num[4],num[6]),format_rt) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) == 11: temp.append(num[-1]) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if ',' in num[0]: total=num[0].replace(',','')+' kb '+num[3].replace(',','')+' kb)' worksheet0.write('g5',total,format_rt) if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if num[0] == 'memory': worksheet0.write('g7',num[-1],format_rt) if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if num[0] == 'role:': if num[1] == 'active,' and num[-1] == 'standby': worksheet0.write('g8',u'正常',format_rt) else: worksheet0.write('g8',u'有',format_red) if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0] == 'current': worksheet0.write('g9',str(num[-1]),format_rt) if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if num[0] == 'destinations' and len(num) == 10: worksheet0.write('g10',str(num[2]),format_rt) if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: if len(num) == 7: if num[1] == 'up': g+=1 if hang > int(hang_list[10]) and hang < int(hang_list[14]): if num!=[]: if num[0] == 'advanced': rules.append(num[3]) if hang > int(hang_list[14]) and hang < int(hang_list[15]): if int(hang_list[-2])-int(hang_list[-3]) == 8: worksheet0.write('g13',u'无告警',format_rt) else: worksheet0.write('g13',u'有告警',format_red) if hang > int(hang_list[15]) and hang < int(hang_list[16]): pass hang = hang + 1 temp.sort() worksheet0.write('g4',u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) worksheet0.write('g6',none,format_rt) worksheet0.write('g11',g,format_rt) worksheet0.write('g12','%s+%s+%s+%s=%d'%(rules[0],rules[1],rules[2],rules[3],int(rules[0])+int(rules[1])+int(rules[2])+int(rules[3])),format_rt) worksheet0.write('g14',u'正常',format_right) except ioerror as reason: print "01dmz-e8000e.py "+str(reason) #------------------------------------------------ #----------firewall_ip2---------------- #------------------------------------------------ try: hang=1 state=dict() hang_list=cmd("<w-12580-m9006>",file_02_2) temp=[] rules=0 mem=[] g=0 all=0 n=0 level=0 q=0 status=0 type=0 with open(file_02_2) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[0]) and hang < int(hang_list[1]): if num!=[]: if num[0].isdigit() and len(num) == 7: if num[3] == 'absent': if num[2] != 'none': type=1 if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if len(num) == 13: worksheet0.write('g16',u'%d天,%s小时'%(int(num[5])*7+int(num[7]),num[9]),format_rt) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if len(num) == 9: temp.append(num[4]) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) == 6: total='%s kb %s kb)'%(num[0],num[3]) worksheet0.write('g18',total,format_rt) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if len(num) == 8: if num[-1] != 'kb:': result='%.1f%%'%(100-float(num[-1].split('%')[0])) mem.append(result) if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if 'gigabitethernet' in num[1]: if num[2] != 'up': status+=1 if 'gigabitethernet' in num[0]: if num[1] != 'up': status+=1 if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if num[0].isdigit() and len(num) == 5: if num[3] != 'primary' and num[3] != 'secondary': q=1 if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0].isdigit() and len(num) == 9: g+=1 if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if num[0] == 'summary': state[n]=num[3] n+=1 if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: if len(num) >= 4 and num[1] == 'up': all+=1 if hang > int(hang_list[10]) and hang < int(hang_list[12]): if num!=[]: if num[0] == 'rule': rules+=1 if hang > int(hang_list[12]) and hang < int(hang_list[13]): if num!=[]: if num[0].isdigit() and len(num) >5: if num[3] != 'info': level=1 if hang > int(hang_list[13]) and hang < int(hang_list[14]): pass hang+=1 temp.sort() mem.sort() if type == 0: worksheet0.write('g15',u'正常',format_rt) else: worksheet0.write('g15','error',format_red) worksheet0.write('g17',u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) worksheet0.write('g19',none,format_rt) worksheet0.write('g20','%s-%s'%(mem[0],mem[-1]),format_rt) if status == 0: worksheet0.write('g21',u'正常',format_rt) else: worksheet0.write('g21',status,format_rt) if q == 0: worksheet0.write('g22',u'正常',format_rt) else: worksheet0.write('g22',u'有',format_red) worksheet0.write('g23',g,format_rt) worksheet0.write('g24',state[0],format_rt) worksheet0.write('g25',all,format_rt) worksheet0.write('g26','%d'%rules,format_rt) if level == 1: worksheet0.write('g27','error',format_red) else: worksheet0.write('g27',u'无告警',format_rt) worksheet0.write('g28',u'正常',format_right) except ioerror as reason: print "02dmz-m9006.py "+str(reason) #------------------------------------------------ #----------firewall_ip3---------------- #------------------------------------------------ try: hang=1 state=dict() hang_list=cmd("<w-sm-m9006>",file_02_1) temp=[] rules=[] mem=[] g=0 all=0 n=0 level=0 q=0 status=0 type=0 with open(file_02_1) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[0]) and hang < int(hang_list[1]): if num!=[]: if num[0].isdigit() and len(num) == 7: if num[3] == 'absent': if num[2] != 'none': type=1 if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if len(num) == 13: worksheet0.write('g30',u'%d天,%s小时'%(int(num[5])*7+int(num[7]),num[9]),format_rt) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if len(num) == 9: temp.append(num[4]) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) == 6: total='%s kb %s kb)'%(num[0],num[3]) worksheet0.write('g32',total,format_rt) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if len(num) == 8: if num[-1] != 'kb:': result='%.1f%%'%(100-float(num[-1].split('%')[0])) mem.append(result) if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if 'gigabitethernet' in num[1]: if num[2] != 'up': status=1 if 'gigabitethernet' in num[0]: if num[1] != 'up': status=1 if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if num[0].isdigit() and len(num) == 5: if num[3] != 'primary' and num[3] != 'secondary': q=1 if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0].isdigit() and len(num) == 9: g+=1 if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if num[0] == 'summary': state[n]=num[3] n+=1 if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: if len(num) >= 4 and num[1] == 'up': all+=1 if hang > int(hang_list[10]) and hang < int(hang_list[12]): if num!=[]: if num[0] == 'advanced': rules.append(num[5]) if hang > int(hang_list[12]) and hang < int(hang_list[13]): if num!=[]: if num[0].isdigit() and len(num) >5: if num[3] != 'info': level=1 if hang > int(hang_list[13]) and hang < int(hang_list[14]): pass hang+=1 temp.sort() mem.sort() if type == 0: worksheet0.write('g29',u'正常',format_rt) else: worksheet0.write('g29','error',format_red) worksheet0.write('g31',u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) worksheet0.write('g33',none,format_rt) worksheet0.write('g34','%s-%s'%(mem[0],mem[-1]),format_rt) if status == 0: worksheet0.write('g35',u'正常',format_rt) else: worksheet0.write('g35','error',format_red) if q == 0: worksheet0.write('g36',u'正常',format_rt) else: worksheet0.write('g36',u'有',format_red) worksheet0.write('g37',g,format_rt) worksheet0.write('g38',state[0],format_rt) worksheet0.write('g39',all,format_rt) worksheet0.write('g40','%s+%s=%d'%(rules[0],rules[1],int(rules[0])+int(rules[1])),format_rt) if level == 1: worksheet0.write('g41','error',format_red) else: worksheet0.write('g41',u'无告警',format_rt) worksheet0.write('g42',u'正常',format_right) except ioerror as reason: print "03sm-m9006.py "+str(reason) #------------------------------------------------ #----------firewall_ip4---------------- #------------------------------------------------ try: hang=1 g=0 hang_list=cmd("w-in-fg3950-1 #",file_03) with open(file_03) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[0]) and hang < int(hang_list[1]): if num!=[]: if num[0] == 'memory': worksheet0.write('g45',num[2],format_rt) if len(num) == 8 and num[1] == 'uptime:': worksheet0.write('g43',u'%s天,%s小时'%(num[2],num[4]),format_rt) if num[0] == 'average': if num[1] == 'network': worksheet0.write('g46',num[-5],format_rt) if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if num[0] == "misc": session_count=num[2].split('=') setup_rate=num[3].split('=') worksheet0.write('g47',session_count[-1],format_rt) worksheet0.write('g48',setup_rate[-1],format_rt) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if 's' in num[0] and len(num) >= 6: g+=1 hang+=1 worksheet0.write('g51',g,format_rt) worksheet0.write('g44',none,format_rt) worksheet0.write('g49',none,format_rt) worksheet0.write('g50',none,format_rt) worksheet0.write('g52',none,format_rt) worksheet0.write('g53',none,format_rt) worksheet0.write('g54',none,format_right) except ioerror as reason: print "04in-fg3950b.py "+str(reason) #------------------------------------------------ #------------switch_ip5---------------- #------------switch_ip6---------------- #------------------------------------------------ try: for i in range(2): hang=1 alarm=0 state=0 status=0 alm=0 temp_list=[] index={0:list(range(2,15)),1:list(range(15,28))} file='/usr/sh/shell/linux/xunjian/'+str(today)+'/0'+str(i+5)+'dmz-s9306-'+str(i+1)+'.txt' hang_list=cmd('<jsyd-wx-12580-dmz-9306-%d>'%(i+1),file) with open(file) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if len(num) == 8 and num[1] == '-': if num[-2] != 'normal': alarm=1 if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if num[0] == 'quidway': worksheet1.write('g'+str(index[i][1]),u'%d天, %s小时'%(int(num[7])*7+int(num[9]),num[11]),format_rt) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) == 6 and num[1] == 'kb': mem=num[0].split(',') free=num[3].split(',') worksheet1.write('g'+str(index[i][2]),'%s kb %s kb)'%(mem[0]+mem[1],free[0]+free[1]),format_rt) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if len(num) == 6 and num[4] == 'max:': worksheet1.write('g'+str(index[i][3]),num[3],format_rt) if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if len(num) == 5 and num[0] == 'memory': worksheet1.write('g'+str(index[i][4]),num[-1],format_rt) if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if len(num) >=6 and num[1] == 'up': state+=1 if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0].isdigit() and len(num) == 5: if i == 0 and num[1] != 'master': status=1 if i == 1 and num[1] != 'backup': status=1 if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if num[0] == 'total': worksheet1.write('g'+str(index[i][7]),num[-1],format_rt) if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: if num[0] == 'dynamic:': worksheet1.write('g'+str(index[i][8]),num[1],format_rt) if hang > int(hang_list[10]) and hang < int(hang_list[11]): if num!=[]: if num[0] == 'destinations' and len(num) == 10: worksheet1.write('g'+str(index[i][9]),num[2],format_rt) if hang > int(hang_list[11]) and hang < int(hang_list[12]): if num!=[]: if len(num) >= 6 and num[-5].isdigit(): temp=num[-1].split('.') temp_list.append(temp[0]) if hang > int(hang_list[12]) and hang < int(hang_list[13]): if num!=[]: if len(num) == 2 and num[0] == 'no' and num[1] == 'alarm': alm = 1 if hang > int(hang_list[13]) and hang < int(hang_list[14]): if num!=[]: pass hang+=1 temp_list.sort() if alarm == 0: worksheet1.write('g'+str(index[i][0]),u'正常',format_rt) else: worksheet1.write('g'+str(index[i][0]),'error',format_red) worksheet1.write('g'+str(index[i][5]),state,format_rt) if i == 0: if status == 0: worksheet1.write('g'+str(index[i][6]),'master',format_rt) else: worksheet1.write('g'+str(index[i][6]),'no master',format_red) else: if status == 0: worksheet1.write('g'+str(index[i][6]),'backup',format_rt) else: worksheet1.write('g'+str(index[i][6]),'no backup',format_red) worksheet1.write('g'+str(index[i][10]),u'%s℃-%s℃'%(temp_list[0],temp_list[-1]),format_rt) if alm == 1: worksheet1.write('g'+str(index[i][11]),u'无告警',format_rt) else: worksheet1.write('g'+str(index[i][11]),u'有告警',format_red) worksheet1.write('g'+str(index[i][12]),u'正常',format_right) except ioerror as reason: print "05_06dmz-s9306.py "+str(reason) #------------------------------------------------ #------------switch_ip7---------------- #------------switch_ip8---------------- #------------------------------------------------ try: fi_list=['07dmz-s12508.txt'] fi_name=['<w-dmz-g1g2-12508>'] state=[] for i in range(1): hang=1 file='/usr/sh/shell/linux/xunjian/'+str(today)+'/'+fi_list[i] hang_list=cmd(fi_name[i],file) brd_status=['master','slave','absent'] brd=0 link=0 master=0 backup=0 count=[] temp=[] alarm=0 status=0 index={0:list(range(28,41))} with open(file) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if num[0].isdigit() and len(num) == 4: if num[2] not in brd_status: brd=1 if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if len(num) == 12: worksheet1.write('g'+str(index[i][1]),u'%d天, %s小时'%(int(num[4])*7+int(num[6]),num[8]),format_rt) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) == 6 and num[1] == 'kb': worksheet1.write('g'+str(index[i][2]),'%s kb %s kb)'%(num[0],num[3]),format_rt) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if num[0] == 'used': worksheet1.write('g'+str(index[i][4]),num[-1],format_rt) if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if 'gigabitethernet' in num[1]: if num[2] != 'up': status+=1 if 'gigabitethernet' in num[0]: if num[1] != 'up': status+=1 if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if len(num) >=2 and num[1] == 'up': link+=1 #if num[0] == '----' and num[5] == 'up': # link+=1 if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0].isdigit(): worksheet1.write('g'+str(index[i][7]),num[0],format_rt) if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if num[0] == 'total' and num[-1].isdigit(): worksheet1.write('g'+str(index[i][8]),num[-1],format_rt) if hang > int(hang_list[9]) and hang < int(hang_list[11]): if num!=[]: if num[0] == 'summary' and num[-1].isdigit(): count.append(num[-1]) if hang > int(hang_list[11]) and hang < int(hang_list[12]): pass if hang > int(hang_list[12]) and hang < int(hang_list[13]): if num!=[]: if len(num) >= 8: if num[3].isdigit(): temp.append(num[3]) if num[0] == '----' and num[1] == 'more': temp.append(num[7]) if hang > int(hang_list[13]) and hang < int(hang_list[14]): if num!=[]: if num[0] == 'no' and num[1] == 'alarm': alarm=1 if hang > int(hang_list[14]) and hang < int(hang_list[15]): if num!=[]: pass hang+=1 temp.sort() if brd == 0: worksheet1.write('g'+str(index[i][0]),u'正常',format_rt) else: worksheet1.write('g'+str(index[i][0]),'error',format_red) if status == 0: worksheet1.write('g'+str(index[i][5]),u'正常',format_rt) else: worksheet1.write('g'+str(index[i][5]),status,format_rt) worksheet1.write('g'+str(index[i][3]),none,format_rt) worksheet1.write('g'+str(index[i][6]),link,format_rt) worksheet1.write('g'+str(index[i][9]),int(count[1])+int(count[-2]),format_rt) worksheet1.write('g'+str(index[i][10]),u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) if alarm == 1: worksheet1.write('g'+str(index[i][11]),u'无告警',format_rt) else: worksheet1.write('g'+str(index[i][11]),'error',format_red) worksheet1.write('g'+str(index[i][12]),u'正常',format_right) except ioerror as reason: print "07_08-s12508-01.py"+str(reason) try: fi_list=['08in-s12508.txt'] fi_name=['<w-in-g7g8-12508>'] state=[] for i in range(1): hang=1 file='/usr/sh/shell/linux/xunjian/'+str(today)+'/'+fi_list[i] hang_list=cmd(fi_name[i],file) brd_status=['master','slave','absent'] brd=0 link=0 master=0 backup=0 count=[] temp=[] alarm=0 status=0 index={0:list(range(41,53))} with open(file) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if num[0].isdigit() and len(num) == 4: if num[2] not in brd_status: brd=1 if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if len(num) == 12: worksheet1.write('g'+str(index[i][1]),u'%d天, %s小时'%(int(num[4])*7+int(num[6]),num[8]),format_rt) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) == 6 and num[1] == 'kb': worksheet1.write('g'+str(index[i][2]),'%s kb %s kb)'%(num[0],num[3]),format_rt) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if num[0] == 'used': worksheet1.write('g'+str(index[i][4]),num[-1],format_rt) if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if 'gigabitethernet' in num[1]: if num[2] != 'up': status+=1 if 'gigabitethernet' in num[0]: if num[1] != 'up': status+=1 if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if len(num) >=2 and num[1] == 'up': link+=1 #if num[0] == '----' and num[5] == 'up': # link+=1 if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0].isdigit(): worksheet1.write('g'+str(index[i][7]),num[0],format_rt) if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if num[0] == 'total' and num[-1].isdigit(): worksheet1.write('g'+str(index[i][8]),num[-1],format_rt) if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: if len(num) >= 8: if num[3].isdigit(): temp.append(num[3]) if num[0] == '----' and num[1] == 'more': temp.append(num[7]) if hang > int(hang_list[10]) and hang < int(hang_list[11]): if num!=[]: if num[0] == 'no' and num[1] == 'alarm': alarm=1 if hang > int(hang_list[11]) and hang < int(hang_list[12]): if num!=[]: pass hang+=1 temp.sort() if brd == 0: worksheet1.write('g'+str(index[i][0]),u'正常',format_rt) else: worksheet1.write('g'+str(index[i][0]),'error',format_red) if status == 0: worksheet1.write('g'+str(index[i][5]),u'正常',format_rt) else: worksheet1.write('g'+str(index[i][5]),status,format_rt) worksheet1.write('g'+str(index[i][3]),none,format_rt) worksheet1.write('g'+str(index[i][6]),link,format_rt) worksheet1.write('g'+str(index[i][9]),u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) if alarm == 1: worksheet1.write('g'+str(index[i][10]),u'无告警',format_rt) else: worksheet1.write('g'+str(index[i][10]),'error',format_red) worksheet1.write('g'+str(index[i][11]),u'正常',format_right) except ioerror as reason: print "07_08-s12508-02.py"+str(reason) #------------------------------------------------ #------------switch_ip9---------------- #------------switch_ip10---------------- #------------------------------------------------ try: fi_list=['09sm-n7k-1.txt','10sm-n7k-2.txt'] fi_name=['w-sm-n7k-1#','w-sm-n7k-2#'] state=[] for i in range(2): hang=1 file='/usr/sh/shell/linux/xunjian/'+str(today)+'/'+fi_list[i] hang_list=cmd1(fi_name[i],file) index={0:list(range(53,66)),1:list(range(66,79))} count1 = 0 count2 = 0 count3 = 0 temp = [] with open(file) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[0]) and hang < int(hang_list[1]): if num!=[]: if num[1] == 'ok': count1 +=1 if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if num[0] == 'kernel': uptime = u'%s天,%s小时'%(num[3],num[5]) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if len(num) == 3: if num[-1] == 'free': free = num[0] if num[-1] == 'total': total = num[0] if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if len(num) >= 3: if num[2] == 'up' or num[4] == 'up' or num[5] == 'up': count2 +=1 if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: pass if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if num[-1].isdigit(): count3 += int(num[-1]) if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if num[-1].isdigit(): worksheet1.write('g'+str(index[i][8]),num[-1],format_rt) if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if len(num) == 5 and num[-1].isdigit(): worksheet1.write('g'+str(index[i][9]),num[-1],format_rt) if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if len(num) >= 6 and num[-2].isdigit(): temp.append(num[-2]) if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: if len(num) >=4 and num[0].isdigit(): status = num[2] if hang > int(hang_list[10]) and hang < int(hang_list[11]): if num!=[]: pass hang+=1 temp.sort() worksheet1.write('g'+str(index[i][0]),count1,format_rt) worksheet1.write('g'+str(index[i][1]),uptime,format_rt) worksheet1.write('g'+str(index[i][2]),'%s kb (%s kb)'%(total,free),format_rt) worksheet1.write('g'+str(index[i][3]),none,format_rt) worksheet1.write('g'+str(index[i][4]),none,format_rt) worksheet1.write('g'+str(index[i][5]),count2,format_rt) worksheet1.write('g'+str(index[i][6]),none,format_rt) worksheet1.write('g'+str(index[i][7]),count3,format_rt) worksheet1.write('g'+str(index[i][10]),u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) if status == 'up': worksheet1.write('g'+str(index[i][11]),u'正常',format_rt) else: worksheet1.write('g'+str(index[i][11]),u'不正常',format_red) worksheet1.write('g'+str(index[i][12]),u'正常',format_right) except exception as reason: print "09_10sm-n7k.py "+str(reason) #------------------------------------------------ #------------route_ip11---------------- #------------route_ip12---------------- #------------------------------------------------ try: fi_list=['11dmz-ne40e-1.txt','12dmz-ne40e-2.txt'] fi_name=['<w-sm-e11-ne40e-01>','<w-sm-e12-ne40e-02>'] device=['dis','ne40e-x8\'s','slot','-'] status=0 for i in range(2): hang=1 phy=0 temp=[] alarm=0 index={0:list(range(2,12)),1:list(range(12,22))} file='/usr/sh/shell/linux/xunjian/'+str(today)+'/'+fi_list[i] hang_list=cmd(fi_name[i],file) with open(file) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[0]) and hang < int(hang_list[1]): if num!=[]: if num[0] not in device: status=1 if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if num[0] == 'huawei': worksheet2.write('g'+str(index[i][1]),u'%s天,%s小时'%(num[4],num[6]),format_rt) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if ',' in num[0]: total=num[0].replace(',','')+' kb '+num[3].replace(',','')+' kb)' worksheet2.write('g'+str(index[i][2]),total,format_rt) if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if num[0] == 'memory' and len(num) == 5: worksheet2.write('g'+str(index[i][4]),num[-1],format_rt) if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if len(num) == 7 and num[1] == 'up': phy+=1 if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if num[0] == 'destinations' and len(num) == 10: worksheet2.write('g'+str(index[i][6]),num[2],format_rt) if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if len(num) >= 7 and num[-1].isdigit(): temp.append(num[-1]) if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0] == 'no' and num[1] == 'alarm': alarm=1 if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: pass hang+=1 temp.sort() if status == 0: worksheet2.write('g'+str(index[i][0]),u'正常',format_rt) else: worksheet2.write('g'+str(index[i][0]),'error',format_red) worksheet2.write('g'+str(index[i][3]),none,format_rt) worksheet2.write('g'+str(index[i][5]),phy,format_rt) worksheet2.write('g'+str(index[i][7]),u'%s℃-%s℃'%(temp[0],temp[-1]),format_rt) if alarm == 1: worksheet2.write('g'+str(index[i][8]),u'无告警',format_rt) else: worksheet2.write('g'+str(index[i][8]),'error',format_red) worksheet2.write('g'+str(index[i][9]),u'正常',format_right) except ioerror as reason: print "11_12.py "+str(reason) #------------------------------------------------ #------------fuzai_ip13----------------- #------------fuzai_ip14----------------- #------------fuzai_ip15----------------- #------------fuzai_ip16----------------- #------------------------------------------------ try: fi_list=['13dmz-rd5412-1.txt','14dmz-rd5412-2.txt','15sm-rd6420-1.txt','16sm-rd6420-2.txt'] fi_name='>> ' state=[] for i in range(4): hang=1 status=0 link=0 temp=[] master=0 backup=0 index={0:list(range(2,12)),1:list(range(12,22)),2:list(range(22,32)),3:list(range(32,42))} file='/usr/sh/shell/linux/xunjian/'+str(today)+'/'+fi_list[i] hang_list=cmd(fi_name,file) with open(file) as f: for each_line in f: num=each_line.split() if hang > int(hang_list[0]) and hang < int(hang_list[1]): if num!=[]: if num[0] == 'switch': worksheet3.write('g'+str(index[i][0]),u'%s天,%s时'%(num[3],num[5]),format_rt) if hang > int(hang_list[1]) and hang < int(hang_list[2]): if num!=[]: if num[0] == 'dual': if num[-1] == "ok": worksheet3.write('g'+str(index[i][1]),u'正常',format_rt) else: worksheet3.write('g'+str(index[i][1]),'error',format_red) if hang > int(hang_list[2]) and hang < int(hang_list[3]): if num!=[]: if num[0].isdigit() and len(num) == 3: if num[-1] != 'operational': status=1 if hang > int(hang_list[3]) and hang < int(hang_list[4]): if num!=[]: if num[0].isdigit() and len(num) == 7: if num[-1] == 'up': link+=1 if hang > int(hang_list[4]) and hang < int(hang_list[5]): if num!=[]: if num[0] == 'real' and num[1] == 'servers' and len(num) == 4: real_servers=num[-1].split('(')[-1].split(')')[0] if num[0] == 'server' and num[1] == 'groups' and len(num) == 4: server_groups=num[-1] if num[0] == 'virtual' and num[1] == 'servers' and len(num) == 4: virtual_servers=num[-1].split('(')[-1].split(')')[0] if num[0] == 'filters' and len(num) == 3: filters=num[-1].split('(')[-1].split(')')[0] if hang > int(hang_list[5]) and hang < int(hang_list[6]): if num!=[]: if num[0] == 'cpuutil1second:': worksheet3.write('g'+str(index[i][5]),num[-1],format_rt) if hang > int(hang_list[6]) and hang < int(hang_list[7]): if num!=[]: if num[0] == 'total:': total=int(num[-2]) if num[0] == 'free:': free=int(num[-2]) if hang > int(hang_list[7]) and hang < int(hang_list[8]): if num!=[]: if num[0] == 'sensor' and len(num) == 6: temp.append(num[3]) if hang > int(hang_list[8]) and hang < int(hang_list[9]): if num!=[]: if len(num) == 10: if num[-1] == 'master': master+=1 if num[-1] == 'backup': backup+=1 if hang > int(hang_list[9]) and hang < int(hang_list[10]): if num!=[]: pass hang+=1 state.append(master) state.append(backup) if status == 0: worksheet3.write('g'+str(index[i][2]),u'正常',format_rt) else: worksheet3.write('g'+str(index[i][2]),'error',format_red) worksheet3.write('g'+str(index[i][3]),link,format_rt) worksheet3.write('g'+str(index[i][4]),'%s/%s/%s/%s'%(real_servers,server_groups,virtual_servers,filters),format_rt) worksheet3.write('g'+str(index[i][6]),'%.2f%%'%(float(total-free)/total*100),format_rt) worksheet3.write('g'+str(index[i][7]),temp[0],format_rt) worksheet3.write('g'+str(index[i][9]),u'正常',format_right) if state[0] == state[3]: worksheet3.write('g10','master',format_rt) worksheet3.write('g20','backup',format_rt) else: worksheet3.write('g10','error',format_red) worksheet3.write('g20','error',format_red) if state[4] == state[-1]: worksheet3.write('g30','master',format_rt) worksheet3.write('g40','backup',format_rt) else: worksheet3.write('g30','error',format_red) worksheet3.write('g40','error',format_red) except ioerror as reason: print "13_16-rd5412-6420.py"+str(reason) workbook.close()
5、总脚本入口
wlxj.sh # 总的脚本入口
#!/bin/bash #-----------网络巡检-------------------- dir=/usr/sh/shell/linux/xunjian/python_shell dir1=/usr/sh/shell/linux/xunjian if [ ! -d $dir1/$(date '+%y%m%d') ];then mkdir -p $dir1/$(date '+%y%m%d') fi _shell() { pass='哈哈哈我是密码1' password='哈哈哈我是密码2' ps1='哈哈哈哈我是密码3' /usr/bin/python $dir/01dmz-e8000e.py 'ip1' $pass $password /usr/bin/python $dir/02_03-m9006.py 'ip2' $pass $password /usr/bin/python $dir/02_03-m9006.py 'ip3' $pass $password /usr/bin/python $dir/04in-fg3950b.py 'ip4' $password /usr/bin/python $dir/05_06dmz-s9306.py 'ip5' $pass $password /usr/bin/python $dir/05_06dmz-s9306.py 'ip6' $pass $password /usr/bin/python $dir/07_08-s12508.py 'ip7' $pass $password /usr/bin/python $dir/07_08-s12508.py 'ip8' $pass $password /usr/bin/python $dir/09_10sm-n7k.py 'ip9' $ps1 /usr/bin/python $dir/09_10sm-n7k.py 'ip10' $ps1 /usr/bin/python $dir/11_12dmz-ne40e.py 'ip11' $password /usr/bin/python $dir/11_12dmz-ne40e.py 'ip12' $password /usr/bin/python $dir/13_16-rd5412_6420.py 'ip13' $password /usr/bin/python $dir/13_16-rd5412_6420.py 'ip14' $password /usr/bin/python $dir/13_16-rd5412_6420.py 'ip15' $password /usr/bin/python $dir/13_16-rd5412_6420.py 'ip16' $password } aa=`ls -l $dir1/$(date '+%y%m%d') | wc -l` year=$(date '+%y') month=$(date '+%m') day=$(date '+%d') if [ $month -lt 10 ];then month=`echo $month | sed 's/0//g'` fi if [ $day -lt 10 ];then day=`echo $day | sed 's/0//g'` fi tday=$(date '+%y.%m.%d') new_tday="$year.$month.$day" zip_tday="$month.$day" _shell # 这边尴尬了,输出的文件有的格式有问题,就得改改了 cp -a $dir1/$(date '+%y%m%d')/02dmz-m9006.txt $dir1/$(date '+%y%m%d')/02dmz-m9006.txt.bak cat $dir1/$(date '+%y%m%d')/02dmz-m9006.txt.bak | tr -s "\r\n" "\n" > $dir1/$(date '+%y%m%d')/02dmz-m9006.txt cp -a $dir1/$(date '+%y%m%d')/03sm-m9006.txt $dir1/$(date '+%y%m%d')/03sm-m9006.txt.bak cat $dir1/$(date '+%y%m%d')/03sm-m9006.txt.bak | tr -s "\r\n" "\n" > $dir1/$(date '+%y%m%d')/03sm-m9006.txt # 得到excel 文件 /usr/bin/python $dir/wangluo_xunjian.py > /dev/null 2>&1 if [ -f $dir1/$(date '+%y%m%d')/网络设备巡检-$tday.xlsx ];then sed -i 's/cmcc.*$//g' $dir1/$(date '+%y%m%d')/*.txt zip -qj $dir1/$(date '+%y%m%d')/$zip_tday.zip $dir1/$(date '+%y%m%d')/*.txt >/dev/null if [ $? -ne 0 ];then /usr/bin/python $dir/mail_zip_error.py >/dev/null 2>&1 else mv $dir1/$(date '+%y%m%d')/网络设备巡检-$tday.xlsx $dir1/$(date '+%y%m%d')/网络设备巡检-$new_tday.xlsx /usr/bin/python $dir/mail.py xixixi@139.com "收件人,多个以分号分隔" "$dir1/$(date '+%y%m%d')/网络设备巡检-$new_tday.xlsx" "$dir1/$(date '+%y%m%d')/$zip_tday.zip" fi else /usr/bin/python $dir/mail_excel_error.py >/dev/null 2>&1 fi
6、加入定时任务
我自己定的每天上午9点
7、成品图
以上这篇对python 网络设备巡检脚本的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: Photoshop打造简单的红色水晶苹果