对Python 网络设备巡检脚本的实例讲解

不言
发布: 2018-05-09 10:45:23
原创
5478 人浏览过

这篇文章主要介绍了关于对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(&#39;0&#39;,&#39; &#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
password=str(sys.argv[3])
 
child=pexpect.spawn(&#39;ssh 用户名@%s&#39;%ip)
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/01DMZ-E8000E.txt&#39;,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
child.sendline("%s"%passwd)
child.expect(&#39;(?i)E8000E-1>&#39;)
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(&#39;%b&#39;)
today=datetime.date.today().strftime(&#39;%Y%m%d&#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
password=str(sys.argv[3])
 
 
if ip == &#39;判断ip,手动打码&#39;:
 txt=&#39;03SM-M9006.txt&#39;
else:
 txt=&#39;02DMZ-M9006.txt&#39;
 
child=pexpect.spawn(&#39;ssh 用户名@%s&#39;%ip)
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/&#39;+txt,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
child.sendline("%s"%passwd)
child.expect(&#39;(?i)M9006>&#39;)
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(&#39;a&#39;)
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 == &#39;判断ip&#39;:
 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(&#39;&#39;)
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(&#39;%Y%m%d&#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
 
child=pexpect.spawn(&#39;ssh 用户@%s&#39;%ip)
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/04IN-FG3950B.txt&#39;,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
child.sendline("%s"%passwd)
child.expect(&#39;W-IN-FG3950-1 #&#39;)
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(&#39;W-IN-FG3950-1 #&#39;)
child.sendline("get route info routing-table static")
child.expect(&#39;--More--&#39;)
child.send(&#39; &#39;)
child.expect(&#39;W-IN-FG3950-1 #&#39;)
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(&#39;%b %d&#39;)
today=datetime.date.today().strftime(&#39;%Y%m%d&#39;)
 
tt=tdy.split()
if int(tt[-1]) < 10:
 tdy=tdy.replace(&#39;0&#39;,&#39; &#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
password=str(sys.argv[3])
 
child=pexpect.spawn(&#39;ssh 用户名@%s&#39;%ip)
 
if ip == &#39;判断ip,不止一个该类型设备&#39;:
 txt=&#39;05DMZ-S9306-1.txt&#39;
else:
 txt=&#39;06DMZ-S9306-2.txt&#39;
 
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/&#39;+txt,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
child.sendline("%s"%passwd)
child.expect(&#39;<JSYD-WX-12580-DMZ-9306&#39;)
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(&#39;%b %d&#39;)
today=datetime.date.today().strftime(&#39;%Y%m%d&#39;)
 
tt=tdy.split()
if int(tt[-1]) < 10:
 tdy=tdy.replace(&#39;0&#39;,&#39; &#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
password=str(sys.argv[3])
 
child=pexpect.spawn(&#39;ssh 用户名@%s&#39;%ip)
 
IP=[&#39;ip1&#39;,&#39;ip2&#39;]
 
if ip == IP[0]:
 txt=&#39;07DMZ-S12508.txt&#39;
 name=&#39;<W-DMZ-G1G2-12508>&#39;
else:
 txt=&#39;08IN-S12508.txt&#39;
 name=&#39;<W-IN-G7G8-12508>&#39;
 
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/&#39;+txt,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
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(&#39;%Y%m%d&#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
 
child=pexpect.spawn(&#39;telnet %s&#39;%ip)
 
 
if ip == &#39;是个ip&#39;:
 txt=&#39;09SM-N7K-1.txt&#39;
 name=&#39;W-SM-N7K-1#&#39;
else:
 txt=&#39;10SM-N7K-2.txt&#39;
 name=&#39;W-SM-N7K-2#&#39;
 
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/&#39;+txt,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;login:&#39;)
child.sendline("发送用户名")
child.expect(&#39;(?i)ssword:&#39;)
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(&#39;%b %d&#39;)
today=datetime.date.today().strftime(&#39;%Y%m%d&#39;)
 
tt=tdy.split()
if int(tt[-1]) < 10:
 tdy=tdy.replace(&#39;0&#39;,&#39; &#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
 
child=pexpect.spawn(&#39;ssh 用户名@%s&#39;%ip)
 
IP=[&#39;ip1&#39;,&#39;ip2&#39;]
 
if ip == IP[0]:
 txt=&#39;11DMZ-NE40E-1.txt&#39;
 name=&#39;<W-SM-E11-NE40E-01>&#39;
elif ip == IP[1]:
 txt=&#39;12DMZ-NE40E-2.txt&#39;
 name=&#39;<W-SM-E12-NE40E-02>&#39;
else:
 print "IP Input Error!"
 sys.exit()
 
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/&#39;+txt,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
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(&#39;%b %d&#39;)
today=datetime.date.today().strftime(&#39;%Y%m%d&#39;)
 
tt=tdy.split()
if int(tt[-1]) < 10:
 tdy=tdy.replace(&#39;0&#39;,&#39; &#39;)
 
ip=str(sys.argv[1])
passwd=str(sys.argv[2])
 
child=pexpect.spawn(&#39;ssh 还是用户名@%s&#39;%ip)
 
IP=[&#39;ip1&#39;,&#39;ip2&#39;,&#39;ip3&#39;,&#39;ip4&#39;]
 
if ip == IP[0]:
 txt=&#39;13DMZ-RD5412-1.txt&#39;
elif ip == IP[1]:
 txt=&#39;14DMZ-RD5412-2.txt&#39;
elif ip == IP[2]:
 txt=&#39;15SM-RD6420-1.txt&#39;
elif ip == IP[3]:
 txt=&#39;16SM-RD6420-2.txt&#39;
else:
 print "IP Input Error!"
 sys.exit()
name=&#39;Standalone ADC(?i)&#39;
 
fout=file(&#39;/usr/sh/shell/linux/xunjian/&#39;+today+&#39;/&#39;+txt,&#39;w&#39;)
child.logfile = fout
child.expect(&#39;(?i)ssword:&#39;)
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(&#39;%Y/%m/%d&#39;)
#命令 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=&#39;application/octet-stream&#39; 
maintype,subtype = ctype.split(&#39;/&#39;,1) 
file_msg=email.MIMEImage.MIMEImage(open(file_name,&#39;rb&#39;).read(),subtype) 
file_msg1=email.MIMEImage.MIMEImage(open(file_name1,&#39;rb&#39;).read(),subtype)
 
## 设置附件头 
basename = os.path.basename(file_name) 
file_msg.add_header(&#39;Content-Disposition&#39;,&#39;attachment&#39;, filename = basename)#修改邮件头 
main_msg.attach(file_msg) 
 
basename1 = os.path.basename(file_name1)
file_msg1.add_header(&#39;Content-Disposition&#39;,&#39;attachment&#39;, filename = basename1)#修改邮件头
main_msg.attach(file_msg1)
 
# 设置根容器属性 
main_msg[&#39;From&#39;] = From 
main_msg[&#39;Reply-to&#39;] = ReplyTo 
#main_msg[&#39;To&#39;] = To 
main_msg[&#39;Subject&#39;] = u"[每日巡检] %s邮件标题" %tday
main_msg[&#39;Date&#39;] = email.Utils.formatdate() 
 
#main_msg[&#39;Bcc&#39;] = To 
# 得到格式化后的完整文本 
fullText = main_msg.as_string( ) 
 
# 用smtp发送邮件 
try: 
 server.sendmail(From, To.split(&#39;;&#39;), 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(&#39;%Y/%m/%d&#39;)
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(&#39;%Y/%m/%d&#39;)
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(&#39;%Y.%m.%d&#39;)
today=datetime.date.today().strftime(&#39;%Y%m%d&#39;)
workbook=xlsxwriter.Workbook(u"/usr/sh/shell/linux/xunjian/%s/网络设备巡检-%s.xlsx"%(today,tday),{&#39;strings_to_numbers&#39;:True})
 
file_01=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/01DMZ-E8000E.txt&#39;
file_02_1=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/03SM-M9006.txt&#39;
file_02_2=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/02DMZ-M9006.txt&#39;
file_03=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/04IN-FG3950B.txt&#39;
 
 
# 新建 sheet
worksheet0=workbook.add_worksheet(u"防火墙")
worksheet1=workbook.add_worksheet(u"交换机")
worksheet2=workbook.add_worksheet(u"路由器")
worksheet3=workbook.add_worksheet(u"负载")
 
# 设置单元格格式(居中,颜色,边框,合并,列宽,列隐藏等)
colour=&#39;#660099&#39;
 
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(&#39;white&#39;)
format_title.set_bg_color(colour)
format_title.set_align(&#39;center&#39;)
format_title.set_valign(&#39;vcenter&#39;)
 
format_nr=workbook.add_format()
format_nr.set_border(1)
format_nr.set_bottom(2)
format_nr.set_align(&#39;center&#39;)
format_nr.set_valign(&#39;vcenter&#39;)
 
format_nrr=workbook.add_format()
format_nrr.set_border(1)
format_nrr.set_valign(&#39;vcenter&#39;)
format_nrr.set_text_wrap()
 
format_bottom=workbook.add_format()
format_bottom.set_border(1)
format_bottom.set_bottom(2)
format_bottom.set_valign(&#39;vcenter&#39;)
format_bottom.set_text_wrap()
 
format_rt=workbook.add_format()
format_rt.set_border(1)
format_rt.set_right(2)
format_rt.set_valign(&#39;left&#39;)
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(&#39;vcenter&#39;)
format_right.set_text_wrap()
 
format_red=workbook.add_format()
format_red.set_font_color(&#39;red&#39;)
format_red.set_border(1)
format_red.set_right(2)
format_red.set_valign(&#39;vcenter&#39;)
 
format_rd=workbook.add_format()
format_rd.set_font_color(&#39;red&#39;)
format_rd.set_border(1)
format_rd.set_right(2)
format_rd.set_bottom(2)
format_rd.set_valign(&#39;vcenter&#39;)
 
# 填写相关固定单元格内容
firewall_title=[u&#39;设备名称&#39;,u&#39;设备类型&#39;,u&#39;IP地址&#39;,u&#39;检查项&#39;,u&#39;命令&#39;,u&#39;说明&#39;,u&#39;检查结果&#39;]
 
firewall_jc=[u&#39;设备状态&#39;,u&#39;运行时长&#39;,u&#39;运行温度&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;冗余组状态&#39;,u&#39;session数目&#39;,u&#39;路由条数&#39;,u&#39;端口状态&#39;,u&#39;策略条数&#39;,u&#39;告警&#39;,u&#39;日志&#39;,u&#39;板卡状态&#39;,u&#39;运行时长&#39;,u&#39;运行温度&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;堆叠状态&#39;,u&#39;冗余组状态&#39;,u&#39;session数目&#39;,u&#39;路由条数&#39;,u&#39;端口状态&#39;,u&#39;策略条数&#39;,u&#39;告警&#39;,u&#39;日志&#39;,u&#39;板卡状态&#39;,u&#39;运行时长&#39;,u&#39;运行温度&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;堆叠状态&#39;,u&#39;冗余组状态&#39;,u&#39;session数目&#39;,u&#39;路由条数&#39;,u&#39;端口状态&#39;,u&#39;策略条数&#39;,u&#39;告警&#39;,u&#39;日志&#39;,u&#39;运行时长&#39;,u&#39;CPU使用率 (BOMC看)&#39;,u&#39;内存使用率&#39;,u&#39;网络流量&#39;,u&#39;session数目&#39;,u&#39;新建session速率&#39;,u&#39;接口状态&#39;,u&#39;冗余组状态&#39;,u&#39;路由条数&#39;,u&#39;日志&#39;,u&#39;策略条数&#39;,u&#39;告警&#39;]
 
firewall_cmd=[&#39;dis device | ex Normal&#39;,&#39;dis version&#39;,&#39;dis temperature&#39;,&#39;dir&#39;,u&#39;在BOMC看&#39;,&#39;dis memory-usage&#39;,&#39;dis hrp state&#39;,&#39;dis firewall session table&#39;,&#39;dis ip routing-table protocol static&#39;,&#39;dis int brief (dis logbuffer | in DOWN)&#39;,&#39;dis acl 3004 (3004-3007)&#39;,&#39;dis alarm all&#39;,&#39;dis logbuffer | in [data]&#39;,&#39;dis device | ex Normal&#39;,&#39;dis version | in uptime&#39;,&#39;dis environment&#39;,&#39;dir | in total&#39;,u&#39;在BOMC看&#39;,&#39;dis memory&#39;,&#39;dis irf link&#39;,&#39;dis redundancy group&#39;,&#39;dis session statistics summary&#39;,&#39;dis ip routing-table protocol static&#39;,&#39;dis int brief (dis logbuffer | in DOWN)&#39;,&#39;dis object-policy ip&#39;,&#39;dis alarm&#39;,&#39;dis logbuffer | in [month]&#39;,&#39;dis device | ex Normal&#39;,&#39;dis version | in uptime&#39;,&#39;dis environment&#39;,&#39;dir | in total&#39;,u&#39;在BOMC看&#39;,&#39;dis memory&#39;,&#39;dis irf link&#39;,&#39;dis redundancy group&#39;,&#39;dis session statistics summary&#39;,&#39;dis ip routing-table protocol static&#39;,&#39;dis int brief (dis logbuffer | in DOWN)&#39;,&#39;dis acl all&#39;,&#39;dis alarm&#39;,&#39;dis logbuffer | in [month]&#39;,&#39;get sys performance status&#39;,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;diagnose sys session stat&#39;,&#39;&#39;,u&#39;网页登录查看&#39;,u&#39;网页登录查看&#39;,&#39;get route info routing table static&#39;,u&#39;网页登录查看&#39;,u&#39;网页登录查看&#39;,u&#39;网页登录查看&#39;]
 
firewall_sm=[u&#39;填写Status是Abnormal的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写温度范围&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写本机和邻居的角色,看有无发生主备切换&#39;,u&#39;填写链接总数&#39;,u&#39;填写路由数目&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;填写策略数目&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,u&#39;填写State为Absent,且Type不为NONE的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写温度范围&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率范围&#39;,u&#39;填写Status不是UP的项&#39;,u&#39;填写Status,看有无发生主备切换&#39;,u&#39;填写链接总数&#39;,u&#39;填写路由数目&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;填写策略数目(所有rule的总数)&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,u&#39;填写State为Absent,且Type不为NONE的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写温度范围&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率范围&#39;,u&#39;填写Status不是UP的项&#39;,u&#39;填写Status,看有无发生主备切换&#39;,u&#39;填写链接总数&#39;,u&#39;填写路由数目&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;填写策略数目(acl 3001+3002的总条目数)&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写30分钟内平均带宽占用(kbps)&#39;,u&#39;填写session_count值&#39;,u&#39;填写setup_rate值&#39;,u&#39;填写启用状态的接口数目&#39;,u&#39;填写冗余状态&#39;,u&#39;填写路由数目&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,u&#39;填写策略数目&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;]
 
 
switch_title=[u&#39;设备名称&#39;,u&#39;设备类型&#39;,u&#39;IP地址&#39;,u&#39;检查项&#39;,u&#39;命令&#39;,u&#39;说明&#39;,u&#39;检查结果&#39;]
 
switch_jc=[u&#39;设备状态&#39;,u&#39;运行时长&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;接口状态&#39;,u&#39;VRRP状态&#39;,u&#39;MAC地址条目数&#39;,u&#39;ARP条目数&#39;,u&#39;路由条目数&#39;,u&#39;运行温度&#39;,u&#39;告警&#39;,u&#39;日志&#39;]
switch_jc1=[u&#39;设备状态&#39;,u&#39;运行时长&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;堆叠状态&#39;,u&#39;接口状态&#39;,u&#39;MAC地址条目数&#39;,u&#39;ARP条目数&#39;,u&#39;路由条目数&#39;,u&#39;运行温度&#39;,u&#39;告警&#39;,u&#39;日志&#39;]
 
switch_jc2=[u&#39;设备状态&#39;,u&#39;运行时长&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;堆叠状态&#39;,u&#39;接口状态&#39;,u&#39;MAC地址条目数&#39;,u&#39;ARP条目数&#39;,u&#39;运行温度&#39;,u&#39;告警&#39;,u&#39;日志&#39;]
 
switch_cmd=[&#39;dis device&#39;,&#39;dis version | in Quidway&#39;,&#39;dir&#39;,&#39;dis cpu-usage&#39;,&#39;dis memory-usage&#39;,&#39;dis int brief (dis logbuffer | in IF_STATE)&#39;,&#39;dis vrrp brief&#39;,&#39;dis mac-address total-number&#39;,&#39;dis arp statistics all&#39;,&#39;dis ip routing-table protocol static&#39;,&#39;dis temperature all&#39;,&#39;dis alarm all&#39;,&#39;dis logbuffer | in [date]&#39;]
 
switch_cmmd=[&#39;dis device | ex Normal&#39;,&#39;dis version | in H3C S12508&#39;,&#39;dir&#39;,u&#39;在BOMC看&#39;,&#39;dis memory&#39;,&#39;dis irf link&#39;,&#39;dis int brief (dis logbuffer reverse | in UPDOWN)&#39;,&#39;dis mac-address count&#39;,&#39;dis arp all count&#39;,&#39;dis ip routing-table vpn-instance Dmz/Inside protocol static&#39;,&#39;dis environment&#39;,&#39;dis alarm&#39;,&#39;dis logbuffer reverse | in [date]&#39;]
 
switch_cmd1=[&#39;dis device | ex Normal&#39;,&#39;dis version | in H3C S12508&#39;,&#39;dir&#39;,u&#39;在BOMC看&#39;,&#39;dis memory&#39;,&#39;dis int brief (dis logbuffer reverse | in UPDOWN)&#39;,&#39;dis vrrp&#39;,&#39;dis mac-address count&#39;,&#39;dis arp all count&#39;,&#39;dis ip routing-table protocol static&#39;,&#39;dis environment&#39;,&#39;dis alarm&#39;,&#39;dis logbuffer reverse | in [date]&#39;]
 
switch_cmd2=[&#39;dis device | ex Normal&#39;,&#39;dis version | in H3C S12508&#39;,&#39;dir&#39;,u&#39;在BOMC看&#39;,&#39;dis memory&#39;,&#39;dis irf link&#39;,&#39;dis int brief (dis logbuffer reverse | in UPDOWN)&#39;,&#39;dis mac-address count&#39;,&#39;dis arp all count&#39;,&#39;dis environment&#39;,&#39;dis alarm&#39;,&#39;dis logbuffer reverse | in [date]&#39;]
 
switch_sm=[u&#39;查看电源、风扇以及板卡状态,填写异常项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率&#39;,u&#39;填写内存使用率&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;查看VRRP主备状态&#39;,u&#39;填写总的mac-address条目数&#39;,u&#39;填写动态和静态ARP条目数&#39;,u&#39;填写路由数目&#39;,u&#39;填写温度范围&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
 
switch_ssm=[u&#39;填写状态异常的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写Status不是UP的项&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;填写总的mac-address条目数&#39;,u&#39;填写总的ARP条目数&#39;,u&#39;填写路由数目(Dmz和Inside路由总和)&#39;,u&#39;填写温度范围&#39;,u&#39;填写设备告警,无则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
 
switch_sm1=[u&#39;填写状态异常的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;查看VRRP主备状态&#39;,u&#39;填写总的mac-address条目数&#39;,u&#39;填写总的ARP条目数&#39;,u&#39;填写路由数目&#39;,u&#39;填写温度范围&#39;,u&#39;填写设备告警,无则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正>常”&#39;]
 
switch_sm2=[u&#39;填写状态异常的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写Status不是UP的项&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;填写总的mac-address条目数&#39;,u&#39;填写总的ARP条目数&#39;,u&#39;填写温度范围&#39;,u&#39;填写设备告警,无则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正>常”&#39;]
 
route_title=[u&#39;设备名称&#39;,u&#39;设备类型&#39;,u&#39;IP地址&#39;,u&#39;检查项&#39;,u&#39;命令&#39;,u&#39;说明&#39;,u&#39;检查结果&#39;]
 
route_jc=[u&#39;设备状态&#39;,u&#39;运行时长&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;接口状态&#39;,u&#39;路由条数&#39;,u&#39;运行温度&#39;,u&#39;告警&#39;,u&#39;日志&#39;]
 
route_cmd=[&#39;dis device | ex Normal&#39;,&#39;dis version | in NE40E-X8 uptime&#39;,&#39;dir&#39;,u&#39;在BOMC看&#39;,&#39;dis memory-usage&#39;,&#39;dis int brief (dis logbuffer | in IF_STATE)&#39;,&#39;dis ip routing-table protocol static&#39;,&#39;dis temperature&#39;,&#39;dis alarm all&#39;,&#39;dis logbuffer | in [date]&#39;]
 
route_sm=[u&#39;填写Status不是“Normal”的项&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;填写路由数目&#39;,u&#39;填写温度范围&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
 
fuzai_title=[u&#39;设备名称&#39;,u&#39;设备类型&#39;,u&#39;IP地址&#39;,u&#39;检查项&#39;,u&#39;命令&#39;,u&#39;说明&#39;,u&#39;检查结果&#39;]
 
fuzai_jc=[u&#39;运行时长&#39;,u&#39;电源状态&#39;,u&#39;风扇状态&#39;,u&#39;端口状态&#39;,u&#39;各表项数值&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;硬件温度&#39;,u&#39;冗余状态&#39;,u&#39;日志&#39;]
 
fuzai_cmd=[&#39;/i/sys/general&#39;,&#39;/i/sys/ps&#39;,&#39;/i/sys/fan&#39;,&#39;/i/link&#39;,&#39;/i/sys/capacity slb&#39;,&#39;/stats/mp/cpu&#39;,&#39;/stats/mp/mem&#39;,&#39;/i/sys/temp&#39;,&#39;/i/l3/vrrp&#39;,&#39;/i/sys/log&#39;]
 
fuzai_sm=[u&#39;填写已运行天数,小时数&#39;,u&#39;填写电源状态&#39;,u&#39;填写风扇状态&#39;,u&#39;填写Link处于UP状态的端口数&#39;,u&#39;填写real servers/server groups/virtual servers/filters当前值&#39;,u&#39;填写CPU使用率&#39;,u&#39;填写内存使用率&#39;,u&#39;填写温度&#39;,u&#39;填写VRRP状态,"master"或者"backup"&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
 
newsmswitch_title=[u&#39;设备状态&#39;,u&#39;运行时长&#39;,u&#39;Flash状态&#39;,u&#39;CPU使用率&#39;,u&#39;内存使用率&#39;,u&#39;接口状态&#39;,u&#39;HSRP状态&#39;,u&#39;MAC地址条目数&#39;,u&#39;ARP条目数&#39;,u&#39;路由条目数&#39;,u&#39;运行温度&#39;,u&#39;VPC状态&#39;,u&#39;日志&#39;]
newsmswitch_cmd=[&#39;sh hardware | in ok&#39;,&#39;sh version | in uptime&#39;,&#39;dir&#39;,u&#39;在BOMC看&#39;,u&#39;在BOMC看&#39;,&#39;sh int brief (sh int brief | in up)&#39;,&#39;sh hsrp brief&#39;,&#39;sh mac address-table count&#39;,&#39;sh ip arp | in number&#39;,&#39;sh ip route static summary&#39;,&#39;sh environment temperature&#39;,&#39;sh vpc&#39;,&#39;sh logging last 30&#39;]
 
newsmswitch_sm=[u&#39;填写状态为OK的模块数&#39;,u&#39;填写已运行天数,小时数&#39;,u&#39;填写total和free的flash大小&#39;,u&#39;填写CPU使用率峰值和平均值&#39;,u&#39;填写内存使用率&#39;,u&#39;填写状态为UP的端口数目&#39;,u&#39;查看HSRP状态(Active local则正常)&#39;,u&#39;填写总的mac-address条目数&#39;,u&#39;填写总的ARP条目数&#39;,u&#39;填写路由数目&#39;,u&#39;填写CurTemp范围&#39;,u&#39;查看VPC状态是否正常&#39;,u&#39;查看日志,无异常则填写"正常"&#39;]
 
# ip 我就隐藏掉啦,虽然也没什么意思
host={"DMZ-E8000E":[u&#39;防火墙&#39;,"ip1"],"DMZ-M9006":[u&#39;防火墙&#39;,&#39;ip2&#39;],"SM-M9006":[u&#39;防火墙&#39;,&#39;ip3&#39;],"IN-FG3950B":[u&#39;防火墙&#39;,"ip4"],"DMZ-S9306-1":[u&#39;交换机&#39;,"ip5"],"DMZ-S9306-2":[u&#39;交换机&#39;,"ip6"],"DMZ-S12508":[u&#39;交换机&#39;,"ip7"],"IN-S12508":[u&#39;交换机&#39;,"ip8"],"SM-S12508-1":[u&#39;交换机&#39;,"ip9"],"SM-S12508-2":[u&#39;交换机&#39;,"ip10"],"DMZ-NE40E-1":[u&#39;路由器&#39;,"ip11"],"DMZ-NE40E-2":[u&#39;路由器&#39;,"ip12"],"DMZ-RD5412-1":[u&#39;负载&#39;,"ip13"],"DMZ-RD5412-2":[u&#39;负载&#39;,"ip14"],"SM-RD6420-1":[u&#39;负载&#39;,"ip15"],"SM-RD6420-2":[u&#39;负载&#39;,"ip16"],"SM-N7K-1":[u"交换机","ip17"],"SM-N7K-2":[u"交换机","ip18"]}
 
# 开始填写固定单元格内容
worksheet0.write_row(&#39;A1&#39;,firewall_title,format_title)
worksheet0.merge_range(&#39;A2:A14&#39;,&#39;DMZ-E8000E&#39;,format_nr)
worksheet0.merge_range(&#39;B2:B14&#39;,host["DMZ-E8000E"][0],format_nr)
worksheet0.merge_range(&#39;C2:C14&#39;,host["DMZ-E8000E"][1],format_nr)
worksheet0.merge_range(&#39;A15:A28&#39;,&#39;DMZ-M9006&#39;,format_nr)
worksheet0.merge_range(&#39;B15:B28&#39;,host["DMZ-M9006"][0],format_nr)
worksheet0.merge_range(&#39;C15:C28&#39;,host["DMZ-M9006"][1],format_nr)
worksheet0.merge_range(&#39;A29:A42&#39;,&#39;SM-M9006&#39;,format_nr)
worksheet0.merge_range(&#39;B29:B42&#39;,host["SM-M9006"][0],format_nr)
worksheet0.merge_range(&#39;C29:C42&#39;,host["SM-M9006"][1],format_nr)
worksheet0.merge_range(&#39;A43:A54&#39;,&#39;IN-FG3950B&#39;,format_nr)
worksheet0.merge_range(&#39;B43:B54&#39;,host["IN-FG3950B"][0],format_nr)
worksheet0.merge_range(&#39;C43:C54&#39;,host["IN-FG3950B"][1],format_nr)
worksheet0.merge_range(&#39;E43:E46&#39;,&#39;get sys performance status&#39;,format_nrr)
worksheet0.merge_range(&#39;E47:E48&#39;,&#39;diagnose sys session stat&#39;,format_nrr)
worksheet0.write_column(&#39;D2&#39;,firewall_jc,format_nrr)
worksheet0.write_column(&#39;E2&#39;,firewall_cmd,format_nrr)
worksheet0.write_column(&#39;F2&#39;,firewall_sm,format_nrr)
worksheet0.write(&#39;D14&#39;,u&#39;日志&#39;,format_bottom)
worksheet0.write(&#39;E14&#39;,&#39;dis logbuffer | in [data]&#39;,format_bottom)
worksheet0.write(&#39;F14&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,format_bottom)
worksheet0.write(&#39;D28&#39;,u&#39;日志&#39;,format_bottom)
worksheet0.write(&#39;E28&#39;,&#39;dis logbuffer reverse | in [month]&#39;,format_bottom)
worksheet0.write(&#39;F28&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,format_bottom)
worksheet0.write(&#39;D42&#39;,u&#39;日志&#39;,format_bottom)
worksheet0.write(&#39;E42&#39;,&#39;dis logbuffer reverse | in [month]&#39;,format_bottom)
worksheet0.write(&#39;F42&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;,format_bottom)
worksheet0.write(&#39;D54&#39;,u&#39;告警&#39;,format_bottom)
worksheet0.write(&#39;E54&#39;,u&#39;网页登录查看&#39;,format_bottom)
worksheet0.write(&#39;F54&#39;,u&#39;填写告警内容,无告警则填写“无告警”&#39;,format_bottom)
worksheet0.set_row(0,30)
worksheet0.set_column(&#39;B:B&#39;,15,None,{&#39;hidden&#39;:1})
worksheet0.set_column(&#39;E:F&#39;,40,None,{&#39;hidden&#39;:1})
worksheet0.set_column(&#39;A:A&#39;,15)
worksheet0.set_column(&#39;C:D&#39;,15)
worksheet0.set_column(&#39;G:G&#39;,40)
 
worksheet1.write_row(&#39;A1&#39;,switch_title,format_title)
worksheet1.merge_range(&#39;A2:A14&#39;,&#39;DMZ-S9306-1&#39;,format_nr)
worksheet1.merge_range(&#39;B2:B14&#39;,host["DMZ-S9306-1"][0],format_nr)
worksheet1.merge_range(&#39;C2:C14&#39;,host["DMZ-S9306-1"][1],format_nr)
worksheet1.merge_range(&#39;A15:A27&#39;,&#39;DMZ-S9306-2&#39;,format_nr)
worksheet1.merge_range(&#39;B15:B27&#39;,host["DMZ-S9306-2"][0],format_nr)
worksheet1.merge_range(&#39;C15:C27&#39;,host["DMZ-S9306-2"][1],format_nr)
worksheet1.merge_range(&#39;A28:A40&#39;,&#39;DMZ-S12508&#39;,format_nr)
worksheet1.merge_range(&#39;B28:B40&#39;,host["DMZ-S12508"][0],format_nr)
worksheet1.merge_range(&#39;C28:C40&#39;,host["DMZ-S12508"][1],format_nr)
worksheet1.merge_range(&#39;A41:A52&#39;,&#39;IN-S12508&#39;,format_nr)
worksheet1.merge_range(&#39;B41:B52&#39;,host["IN-S12508"][0],format_nr)
worksheet1.merge_range(&#39;C41:C52&#39;,host["IN-S12508"][1],format_nr)
 
worksheet1.merge_range(&#39;A53:A65&#39;,&#39;SM-N7K-1&#39;,format_nr)
worksheet1.merge_range(&#39;B53:B65&#39;,host["SM-N7K-1"][0],format_nr)
worksheet1.merge_range(&#39;C53:C65&#39;,host["SM-N7K-1"][1],format_nr)
worksheet1.merge_range(&#39;A66:A78&#39;,&#39;SM-N7K-2&#39;,format_nr)
worksheet1.merge_range(&#39;B66:B78&#39;,host["SM-N7K-2"][0],format_nr)
worksheet1.merge_range(&#39;C66:C78&#39;,host["SM-N7K-2"][1],format_nr)
 
worksheet1.write_column(&#39;D2&#39;,switch_jc,format_nrr)
worksheet1.write_column(&#39;D15&#39;,switch_jc,format_nrr)
worksheet1.write_column(&#39;D28&#39;,switch_jc1,format_nrr)
worksheet1.write_column(&#39;D41&#39;,switch_jc2,format_nrr)
worksheet1.write_column(&#39;D53&#39;,newsmswitch_title,format_nrr)
worksheet1.write_column(&#39;D66&#39;,newsmswitch_title,format_nrr)
worksheet1.write_column(&#39;E2&#39;,switch_cmd,format_nrr)
worksheet1.write_column(&#39;E15&#39;,switch_cmd,format_nrr)
worksheet1.write_column(&#39;E28&#39;,switch_cmmd,format_nrr)
worksheet1.write_column(&#39;E41&#39;,switch_cmd2,format_nrr)
worksheet1.write_column(&#39;E53&#39;,newsmswitch_cmd,format_nrr)
worksheet1.write_column(&#39;E66&#39;,newsmswitch_cmd,format_nrr)
worksheet1.write_column(&#39;F2&#39;,switch_sm,format_nrr)
worksheet1.write_column(&#39;F15&#39;,switch_sm,format_nrr)
worksheet1.write_column(&#39;F28&#39;,switch_ssm,format_nrr)
worksheet1.write_column(&#39;F41&#39;,switch_sm2,format_nrr)
worksheet1.write_column(&#39;F53&#39;,newsmswitch_sm,format_nrr)
worksheet1.write_column(&#39;F66&#39;,newsmswitch_sm,format_nrr)
worksheet1.set_row(0,30)
worksheet1.set_column(&#39;B:B&#39;,15,None,{&#39;hidden&#39;:1})
worksheet1.set_column(&#39;E:F&#39;,40,None,{&#39;hidden&#39;:1})
worksheet1.set_column(&#39;A:A&#39;,15)
worksheet1.set_column(&#39;C:D&#39;,15)
worksheet1.set_column(&#39;G:G&#39;,40)
 
 
worksheet2.write_row(&#39;A1&#39;,route_title,format_title)
worksheet2.merge_range(&#39;A2:A11&#39;,&#39;DMZ-NE40E-1&#39;,format_nr)
worksheet2.merge_range(&#39;B2:B11&#39;,host["DMZ-NE40E-1"][0],format_nr)
worksheet2.merge_range(&#39;C2:C11&#39;,host["DMZ-NE40E-1"][1],format_nr)
worksheet2.merge_range(&#39;A12:A21&#39;,&#39;DMZ-NE40E-2&#39;,format_nr)
worksheet2.merge_range(&#39;B12:B21&#39;,host["DMZ-NE40E-2"][0],format_nr)
worksheet2.merge_range(&#39;C12:C21&#39;,host["DMZ-NE40E-2"][1],format_nr)
worksheet2.write_column(&#39;D2&#39;,route_jc,format_nrr)
worksheet2.write_column(&#39;D12&#39;,route_jc,format_nrr)
worksheet2.write_column(&#39;E2&#39;,route_cmd,format_nrr)
worksheet2.write_column(&#39;E12&#39;,route_cmd,format_nrr)
worksheet2.write_column(&#39;F2&#39;,route_sm,format_nrr)
worksheet2.write_column(&#39;F12&#39;,route_sm,format_nrr)
worksheet2.set_row(0,30)
worksheet2.set_column(&#39;B:B&#39;,15,None,{&#39;hidden&#39;:1})
worksheet2.set_column(&#39;E:F&#39;,40,None,{&#39;hidden&#39;:1})
worksheet2.set_column(&#39;A:A&#39;,15)
worksheet2.set_column(&#39;C:D&#39;,15)
worksheet2.set_column(&#39;G:G&#39;,40)
 
worksheet3.write_row(&#39;A1&#39;,fuzai_title,format_title)
worksheet3.merge_range(&#39;A2:A11&#39;,&#39;DMZ-RD5412-1&#39;,format_nr)
worksheet3.merge_range(&#39;B2:B11&#39;,host["DMZ-RD5412-1"][0],format_nr)
worksheet3.merge_range(&#39;C2:C11&#39;,host["DMZ-RD5412-1"][1],format_nr)
worksheet3.merge_range(&#39;A12:A21&#39;,&#39;DMZ-RD5412-2&#39;,format_nr)
worksheet3.merge_range(&#39;B12:B21&#39;,host["DMZ-RD5412-2"][0],format_nr)
worksheet3.merge_range(&#39;C12:C21&#39;,host["DMZ-RD5412-2"][1],format_nr)
worksheet3.merge_range(&#39;A22:A31&#39;,&#39;SM-RD6420-1&#39;,format_nr)
worksheet3.merge_range(&#39;B22:B31&#39;,host["SM-RD6420-1"][0],format_nr)
worksheet3.merge_range(&#39;C22:C31&#39;,host["SM-RD6420-1"][1],format_nr)
worksheet3.merge_range(&#39;A32:A41&#39;,&#39;SM-RD6420-2&#39;,format_nr)
worksheet3.merge_range(&#39;B32:B41&#39;,host["SM-RD6420-2"][0],format_nr)
worksheet3.merge_range(&#39;C32:C41&#39;,host["SM-RD6420-2"][1],format_nr)
worksheet3.write_column(&#39;D2&#39;,fuzai_jc,format_nrr)
worksheet3.write_column(&#39;D12&#39;,fuzai_jc,format_nrr)
worksheet3.write_column(&#39;D22&#39;,fuzai_jc,format_nrr)
worksheet3.write_column(&#39;D32&#39;,fuzai_jc,format_nrr)
worksheet3.write_column(&#39;E2&#39;,fuzai_cmd,format_nrr)
worksheet3.write_column(&#39;E12&#39;,fuzai_cmd,format_nrr)
worksheet3.write_column(&#39;E22&#39;,fuzai_cmd,format_nrr)
worksheet3.write_column(&#39;E32&#39;,fuzai_cmd,format_nrr)
worksheet3.write_column(&#39;F2&#39;,fuzai_sm,format_nrr)
worksheet3.write_column(&#39;F12&#39;,fuzai_sm,format_nrr)
worksheet3.write_column(&#39;F22&#39;,fuzai_sm,format_nrr)
worksheet3.write_column(&#39;F32&#39;,fuzai_sm,format_nrr)
worksheet3.set_row(0,30)
worksheet3.set_column(&#39;B:B&#39;,15,None,{&#39;hidden&#39;:1})
worksheet3.set_column(&#39;E:F&#39;,40,None,{&#39;hidden&#39;:1})
worksheet3.set_column(&#39;A:A&#39;,15)
worksheet3.set_column(&#39;C:D&#39;,15)
worksheet3.set_column(&#39;G:G&#39;,40)
 
jcx=[u&#39;日志&#39;,&#39;dis logbuffer | in [date]&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
jcx1=[u&#39;日志&#39;,&#39;dis logbuffer reverse | in [date]&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
jcx2=[u&#39;日志&#39;,&#39;sh logging last 30&#39;,u&#39;查看日志,无异常则填写“正常”&#39;]
 
fuzai_jcx=[u&#39;日志&#39;,&#39;/i/sys/log&#39;,u&#39;填写异常日志,无异常则填写“正常”&#39;]
worksheet1.write_row(&#39;D14&#39;,jcx,format_bottom)
worksheet1.write_row(&#39;D27&#39;,jcx,format_bottom)
worksheet1.write_row(&#39;D40&#39;,jcx,format_bottom)
worksheet1.write_row(&#39;D52&#39;,jcx1,format_bottom)
worksheet1.write_row(&#39;D65&#39;,jcx2,format_bottom)
worksheet1.write_row(&#39;D78&#39;,jcx2,format_bottom)
worksheet2.write_row(&#39;D11&#39;,jcx,format_bottom)
worksheet2.write_row(&#39;D21&#39;,jcx,format_bottom)
worksheet3.write_row(&#39;D11&#39;,fuzai_jcx,format_bottom)
worksheet3.write_row(&#39;D21&#39;,fuzai_jcx,format_bottom)
worksheet3.write_row(&#39;D31&#39;,fuzai_jcx,format_bottom)
worksheet3.write_row(&#39;D41&#39;,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(&#39;G2&#39;,u&#39;正常&#39;,format_rt)
  else:
  worksheet0.write(&#39;G2&#39;,&#39;ERROR&#39;,format_red)
  if hang > int(hang_list[2]) and hang < int(hang_list[3]):
  if num!=[]:
  if num[0] == &#39;HUAWEI&#39;:
   worksheet0.write(&#39;G3&#39;,u&#39;%s天,%s小时&#39;%(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 &#39;,&#39; in num[0]:
   total=num[0].replace(&#39;,&#39;,&#39;&#39;)+&#39; KB &#39;+num[3].replace(&#39;,&#39;,&#39;&#39;)+&#39; KB)&#39;
   worksheet0.write(&#39;G5&#39;,total,format_rt)
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
  if num[0] == &#39;Memory&#39;:
   worksheet0.write(&#39;G7&#39;,num[-1],format_rt)
  if hang > int(hang_list[6]) and hang < int(hang_list[7]):
  if num!=[]:
  if num[0] == &#39;Role:&#39;:
   if num[1] == &#39;active,&#39; and num[-1] == &#39;standby&#39;:
   worksheet0.write(&#39;G8&#39;,u&#39;正常&#39;,format_rt)
   else:
   worksheet0.write(&#39;G8&#39;,u&#39;有&#39;,format_red)
  if hang > int(hang_list[7]) and hang < int(hang_list[8]):
  if num!=[]:
  if num[0] == &#39;Current&#39;:
   worksheet0.write(&#39;G9&#39;,str(num[-1]),format_rt)
  if hang > int(hang_list[8]) and hang < int(hang_list[9]):
  if num!=[]:
  if num[0] == &#39;Destinations&#39; and len(num) == 10:
   worksheet0.write(&#39;G10&#39;,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] == &#39;up&#39;:
   g+=1
  if hang > int(hang_list[10]) and hang < int(hang_list[14]):
  if num!=[]:
  if num[0] == &#39;Advanced&#39;:
   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(&#39;G13&#39;,u&#39;无告警&#39;,format_rt)
  else:
  worksheet0.write(&#39;G13&#39;,u&#39;有告警&#39;,format_red)
  if hang > int(hang_list[15]) and hang < int(hang_list[16]):
  pass
  hang = hang + 1
 temp.sort()
 worksheet0.write(&#39;G4&#39;,u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
 worksheet0.write(&#39;G6&#39;,None,format_rt)
 worksheet0.write(&#39;G11&#39;,g,format_rt)
 worksheet0.write(&#39;G12&#39;,&#39;%s+%s+%s+%s=%d&#39;%(rules[0],rules[1],rules[2],rules[3],int(rules[0])+int(rules[1])+int(rules[2])+int(rules[3])),format_rt)
 worksheet0.write(&#39;G14&#39;,u&#39;正常&#39;,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] == &#39;Absent&#39;:
   if num[2] != &#39;NONE&#39;:
   type=1
  if hang > int(hang_list[1]) and hang < int(hang_list[2]):
  if num!=[]:
  if len(num) == 13:
   worksheet0.write(&#39;G16&#39;,u&#39;%d天,%s小时&#39;%(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=&#39;%s KB %s KB)&#39;%(num[0],num[3])
   worksheet0.write(&#39;G18&#39;,total,format_rt)
  if hang > int(hang_list[4]) and hang < int(hang_list[5]):
  if num!=[]:
  if len(num) == 8:
   if num[-1] != &#39;KB:&#39;:
   result=&#39;%.1f%%&#39;%(100-float(num[-1].split(&#39;%&#39;)[0]))
   mem.append(result)
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
  if &#39;GigabitEthernet&#39; in num[1]:
   if num[2] != &#39;UP&#39;:
   status+=1
  if &#39;GigabitEthernet&#39; in num[0]:
   if num[1] != &#39;UP&#39;:
   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] != &#39;Primary&#39; and num[3] != &#39;Secondary&#39;:
   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] == &#39;Summary&#39;:
   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] == &#39;UP&#39;:
   all+=1
  if hang > int(hang_list[10]) and hang < int(hang_list[12]):
  if num!=[]:
  if num[0] == &#39;rule&#39;:
   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] != &#39;INFO&#39;:
   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(&#39;G15&#39;,u&#39;正常&#39;,format_rt)
 else:
  worksheet0.write(&#39;G15&#39;,&#39;ERROR&#39;,format_red)
 worksheet0.write(&#39;G17&#39;,u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
 worksheet0.write(&#39;G19&#39;,None,format_rt)
 worksheet0.write(&#39;G20&#39;,&#39;%s-%s&#39;%(mem[0],mem[-1]),format_rt)
 if status == 0:
  worksheet0.write(&#39;G21&#39;,u&#39;正常&#39;,format_rt)
 else:
  worksheet0.write(&#39;G21&#39;,status,format_rt)
 if q == 0:
  worksheet0.write(&#39;G22&#39;,u&#39;正常&#39;,format_rt)
 else:
  worksheet0.write(&#39;G22&#39;,u&#39;有&#39;,format_red)
 worksheet0.write(&#39;G23&#39;,g,format_rt)
 worksheet0.write(&#39;G24&#39;,state[0],format_rt)
 worksheet0.write(&#39;G25&#39;,all,format_rt)
 worksheet0.write(&#39;G26&#39;,&#39;%d&#39;%rules,format_rt)
 if level == 1:
  worksheet0.write(&#39;G27&#39;,&#39;ERROR&#39;,format_red)
 else:
  worksheet0.write(&#39;G27&#39;,u&#39;无告警&#39;,format_rt)
 worksheet0.write(&#39;G28&#39;,u&#39;正常&#39;,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] == &#39;Absent&#39;:
   if num[2] != &#39;NONE&#39;:
   type=1
  if hang > int(hang_list[1]) and hang < int(hang_list[2]):
  if num!=[]:
  if len(num) == 13:
   worksheet0.write(&#39;G30&#39;,u&#39;%d天,%s小时&#39;%(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=&#39;%s KB %s KB)&#39;%(num[0],num[3])
   worksheet0.write(&#39;G32&#39;,total,format_rt)
  if hang > int(hang_list[4]) and hang < int(hang_list[5]):
  if num!=[]:
  if len(num) == 8:
   if num[-1] != &#39;KB:&#39;:
   result=&#39;%.1f%%&#39;%(100-float(num[-1].split(&#39;%&#39;)[0]))
   mem.append(result) 
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
  if &#39;GigabitEthernet&#39; in num[1]:
   if num[2] != &#39;UP&#39;:
   status=1
  if &#39;GigabitEthernet&#39; in num[0]:
   if num[1] != &#39;UP&#39;:
   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] != &#39;Primary&#39; and num[3] != &#39;Secondary&#39;:
   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] == &#39;Summary&#39;:
   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] == &#39;UP&#39;:
   all+=1
  if hang > int(hang_list[10]) and hang < int(hang_list[12]):
  if num!=[]:
  if num[0] == &#39;Advanced&#39;:
   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] != &#39;INFO&#39;:
   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(&#39;G29&#39;,u&#39;正常&#39;,format_rt)
 else:
  worksheet0.write(&#39;G29&#39;,&#39;ERROR&#39;,format_red)
 worksheet0.write(&#39;G31&#39;,u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
 worksheet0.write(&#39;G33&#39;,None,format_rt)
 worksheet0.write(&#39;G34&#39;,&#39;%s-%s&#39;%(mem[0],mem[-1]),format_rt)
 if status == 0:
  worksheet0.write(&#39;G35&#39;,u&#39;正常&#39;,format_rt)
 else:
  worksheet0.write(&#39;G35&#39;,&#39;ERROR&#39;,format_red)
 if q == 0:
  worksheet0.write(&#39;G36&#39;,u&#39;正常&#39;,format_rt)
 else:
  worksheet0.write(&#39;G36&#39;,u&#39;有&#39;,format_red)
 worksheet0.write(&#39;G37&#39;,g,format_rt)
 worksheet0.write(&#39;G38&#39;,state[0],format_rt)
 worksheet0.write(&#39;G39&#39;,all,format_rt)
 worksheet0.write(&#39;G40&#39;,&#39;%s+%s=%d&#39;%(rules[0],rules[1],int(rules[0])+int(rules[1])),format_rt)
 if level == 1:
  worksheet0.write(&#39;G41&#39;,&#39;ERROR&#39;,format_red)
 else:
  worksheet0.write(&#39;G41&#39;,u&#39;无告警&#39;,format_rt)
 worksheet0.write(&#39;G42&#39;,u&#39;正常&#39;,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] == &#39;Memory&#39;:
   worksheet0.write(&#39;G45&#39;,num[2],format_rt)
  if len(num) == 8 and num[1] == &#39;Uptime:&#39;:
   worksheet0.write(&#39;G43&#39;,u&#39;%s天,%s小时&#39;%(num[2],num[4]),format_rt)
  if num[0] == &#39;Average&#39;:
   if num[1] == &#39;network&#39;:
   worksheet0.write(&#39;G46&#39;,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(&#39;=&#39;)
   setup_rate=num[3].split(&#39;=&#39;)
   worksheet0.write(&#39;G47&#39;,session_count[-1],format_rt)
   worksheet0.write(&#39;G48&#39;,setup_rate[-1],format_rt)
  if hang > int(hang_list[2]) and hang < int(hang_list[3]):
  if num!=[]:
  if &#39;S&#39; in num[0] and len(num) >= 6:
   g+=1
  hang+=1
 worksheet0.write(&#39;G51&#39;,g,format_rt)
 worksheet0.write(&#39;G44&#39;,None,format_rt)
 worksheet0.write(&#39;G49&#39;,None,format_rt)
 worksheet0.write(&#39;G50&#39;,None,format_rt)
 worksheet0.write(&#39;G52&#39;,None,format_rt)
 worksheet0.write(&#39;G53&#39;,None,format_rt)
 worksheet0.write(&#39;G54&#39;,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=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/0&#39;+str(i+5)+&#39;DMZ-S9306-&#39;+str(i+1)+&#39;.txt&#39;
 hang_list=cmd(&#39;<JSYD-WX-12580-DMZ-9306-%d>&#39;%(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] == &#39;-&#39;:
   if num[-2] != &#39;Normal&#39;:
   alarm=1
  if hang > int(hang_list[2]) and hang < int(hang_list[3]):
  if num!=[]:
   if num[0] == &#39;Quidway&#39;:
   worksheet1.write(&#39;G&#39;+str(index[i][1]),u&#39;%d天, %s小时&#39;%(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] == &#39;KB&#39;:
   mem=num[0].split(&#39;,&#39;)
   free=num[3].split(&#39;,&#39;)
   worksheet1.write(&#39;G&#39;+str(index[i][2]),&#39;%s KB %s KB)&#39;%(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] == &#39;Max:&#39;:
   worksheet1.write(&#39;G&#39;+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] == &#39;Memory&#39;:
   worksheet1.write(&#39;G&#39;+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] == &#39;up&#39;:
   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] != &#39;Master&#39;:
   status=1
   if i == 1 and num[1] != &#39;Backup&#39;:
   status=1
  if hang > int(hang_list[8]) and hang < int(hang_list[9]):
  if num!=[]:
   if num[0] == &#39;Total&#39;:
   worksheet1.write(&#39;G&#39;+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] == &#39;Dynamic:&#39;:
   worksheet1.write(&#39;G&#39;+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] == &#39;Destinations&#39; and len(num) == 10:
   worksheet1.write(&#39;G&#39;+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(&#39;.&#39;)
   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] == &#39;NO&#39; and num[1] == &#39;alarm&#39;:
   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(&#39;G&#39;+str(index[i][0]),u&#39;正常&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][0]),&#39;ERROR&#39;,format_red)
  worksheet1.write(&#39;G&#39;+str(index[i][5]),state,format_rt)
  if i == 0:
  if status == 0:
  worksheet1.write(&#39;G&#39;+str(index[i][6]),&#39;Master&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][6]),&#39;No Master&#39;,format_red)
  else:
  if status == 0:
  worksheet1.write(&#39;G&#39;+str(index[i][6]),&#39;Backup&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][6]),&#39;No Backup&#39;,format_red)
  worksheet1.write(&#39;G&#39;+str(index[i][10]),u&#39;%s℃-%s℃&#39;%(temp_list[0],temp_list[-1]),format_rt)
  if alm == 1:
  worksheet1.write(&#39;G&#39;+str(index[i][11]),u&#39;无告警&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][11]),u&#39;有告警&#39;,format_red)
  worksheet1.write(&#39;G&#39;+str(index[i][12]),u&#39;正常&#39;,format_right)
  
except IOError as reason:
 print "05_06DMZ-S9306.py "+str(reason)
 
#------------------------------------------------
#------------switch_ip7----------------
#------------switch_ip8----------------
#------------------------------------------------
 
try:
 fi_list=[&#39;07DMZ-S12508.txt&#39;]
 fi_name=[&#39;<W-DMZ-G1G2-12508>&#39;]
 state=[]
 for i in range(1):
 hang=1
 file=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/&#39;+fi_list[i]
 hang_list=cmd(fi_name[i],file)
 brd_status=[&#39;Master&#39;,&#39;Slave&#39;,&#39;Absent&#39;]
 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(&#39;G&#39;+str(index[i][1]),u&#39;%d天, %s小时&#39;%(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] == &#39;KB&#39;:
   worksheet1.write(&#39;G&#39;+str(index[i][2]),&#39;%s KB %s KB)&#39;%(num[0],num[3]),format_rt)
  if hang > int(hang_list[4]) and hang < int(hang_list[5]):
  if num!=[]:
   if num[0] == &#39;Used&#39;:
   worksheet1.write(&#39;G&#39;+str(index[i][4]),num[-1],format_rt)
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
   if &#39;GigabitEthernet&#39; in num[1]:
   if num[2] != &#39;UP&#39;:
   status+=1
   if &#39;GigabitEthernet&#39; in num[0]:
   if num[1] != &#39;UP&#39;:
   status+=1 
  if hang > int(hang_list[6]) and hang < int(hang_list[7]):
  if num!=[]:
   if len(num) >=2 and num[1] == &#39;UP&#39;:
   link+=1
   #if num[0] == &#39;----&#39; and num[5] == &#39;UP&#39;:
   # link+=1
  if hang > int(hang_list[7]) and hang < int(hang_list[8]):
  if num!=[]:
   if num[0].isdigit():
   worksheet1.write(&#39;G&#39;+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] == &#39;Total&#39; and num[-1].isdigit():
   worksheet1.write(&#39;G&#39;+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] == &#39;Summary&#39; 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] == &#39;----&#39; and num[1] == &#39;More&#39;:
   temp.append(num[7])
  if hang > int(hang_list[13]) and hang < int(hang_list[14]):
  if num!=[]:
   if num[0] == &#39;No&#39; and num[1] == &#39;alarm&#39;:
   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(&#39;G&#39;+str(index[i][0]),u&#39;正常&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][0]),&#39;ERROR&#39;,format_red)
  if status == 0:
  worksheet1.write(&#39;G&#39;+str(index[i][5]),u&#39;正常&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][5]),status,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][3]),None,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][6]),link,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][9]),int(count[1])+int(count[-2]),format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][10]),u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
  if alarm == 1:
  worksheet1.write(&#39;G&#39;+str(index[i][11]),u&#39;无告警&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][11]),&#39;Error&#39;,format_red)
  worksheet1.write(&#39;G&#39;+str(index[i][12]),u&#39;正常&#39;,format_right)
except IOError as reason:
 print "07_08-S12508-01.py"+str(reason)
 
 
try:
 fi_list=[&#39;08IN-S12508.txt&#39;]
 fi_name=[&#39;<W-IN-G7G8-12508>&#39;]
 state=[]
 for i in range(1):
 hang=1
 file=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/&#39;+fi_list[i]
 hang_list=cmd(fi_name[i],file)
 brd_status=[&#39;Master&#39;,&#39;Slave&#39;,&#39;Absent&#39;]
 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(&#39;G&#39;+str(index[i][1]),u&#39;%d天, %s小时&#39;%(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] == &#39;KB&#39;:
   worksheet1.write(&#39;G&#39;+str(index[i][2]),&#39;%s KB %s KB)&#39;%(num[0],num[3]),format_rt)
  if hang > int(hang_list[4]) and hang < int(hang_list[5]):
  if num!=[]:
   if num[0] == &#39;Used&#39;:
   worksheet1.write(&#39;G&#39;+str(index[i][4]),num[-1],format_rt)
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
   if &#39;GigabitEthernet&#39; in num[1]:
   if num[2] != &#39;UP&#39;:
   status+=1
   if &#39;GigabitEthernet&#39; in num[0]:
   if num[1] != &#39;UP&#39;:
   status+=1
  if hang > int(hang_list[6]) and hang < int(hang_list[7]):
  if num!=[]:
   if len(num) >=2 and num[1] == &#39;UP&#39;:
   link+=1
   #if num[0] == &#39;----&#39; and num[5] == &#39;UP&#39;:
   # link+=1
  if hang > int(hang_list[7]) and hang < int(hang_list[8]):
  if num!=[]:
   if num[0].isdigit():
   worksheet1.write(&#39;G&#39;+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] == &#39;Total&#39; and num[-1].isdigit():
   worksheet1.write(&#39;G&#39;+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] == &#39;----&#39; and num[1] == &#39;More&#39;:
   temp.append(num[7])
  if hang > int(hang_list[10]) and hang < int(hang_list[11]):
  if num!=[]:
   if num[0] == &#39;No&#39; and num[1] == &#39;alarm&#39;:
   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(&#39;G&#39;+str(index[i][0]),u&#39;正常&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][0]),&#39;ERROR&#39;,format_red)
  if status == 0:
  worksheet1.write(&#39;G&#39;+str(index[i][5]),u&#39;正常&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][5]),status,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][3]),None,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][6]),link,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][9]),u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
  if alarm == 1:
  worksheet1.write(&#39;G&#39;+str(index[i][10]),u&#39;无告警&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][10]),&#39;Error&#39;,format_red)
  worksheet1.write(&#39;G&#39;+str(index[i][11]),u&#39;正常&#39;,format_right)
except IOError as reason:
 print "07_08-S12508-02.py"+str(reason)
 
 
 
#------------------------------------------------
#------------switch_ip9----------------
#------------switch_ip10----------------
#------------------------------------------------
 
try:
 fi_list=[&#39;09SM-N7K-1.txt&#39;,&#39;10SM-N7K-2.txt&#39;]
 fi_name=[&#39;W-SM-N7K-1#&#39;,&#39;W-SM-N7K-2#&#39;]
 state=[]
 for i in range(2):
 hang=1
 file=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/&#39;+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] == &#39;ok&#39;:
   count1 +=1
  if hang > int(hang_list[1]) and hang < int(hang_list[2]):
  if num!=[]:
   if num[0] == &#39;Kernel&#39;:
   uptime = u&#39;%s天,%s小时&#39;%(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] == &#39;free&#39;:
   free = num[0]
   if num[-1] == &#39;total&#39;:
   total = num[0]
  if hang > int(hang_list[3]) and hang < int(hang_list[4]):
  if num!=[]:
   if len(num) >= 3:
   if num[2] == &#39;up&#39; or num[4] == &#39;up&#39; or num[5] == &#39;up&#39;:
   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(&#39;G&#39;+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(&#39;G&#39;+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(&#39;G&#39;+str(index[i][0]),count1,format_rt) 
  worksheet1.write(&#39;G&#39;+str(index[i][1]),uptime,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][2]),&#39;%s KB (%s KB)&#39;%(total,free),format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][3]),None,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][4]),None,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][5]),count2,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][6]),None,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][7]),count3,format_rt)
  worksheet1.write(&#39;G&#39;+str(index[i][10]),u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
  if status == &#39;up&#39;:
  worksheet1.write(&#39;G&#39;+str(index[i][11]),u&#39;正常&#39;,format_rt)
  else:
  worksheet1.write(&#39;G&#39;+str(index[i][11]),u&#39;不正常&#39;,format_red)
  worksheet1.write(&#39;G&#39;+str(index[i][12]),u&#39;正常&#39;,format_right)
except Exception as reason:
 print "09_10SM-N7K.py "+str(reason)
 
 
#------------------------------------------------
#------------route_ip11----------------
#------------route_ip12----------------
#------------------------------------------------
 
try:
 fi_list=[&#39;11DMZ-NE40E-1.txt&#39;,&#39;12DMZ-NE40E-2.txt&#39;]
 fi_name=[&#39;<W-SM-E11-NE40E-01>&#39;,&#39;<W-SM-E12-NE40E-02>&#39;]
 device=[&#39;dis&#39;,&#39;NE40E-X8\&#39;s&#39;,&#39;Slot&#39;,&#39;-&#39;]
 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=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/&#39;+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] == &#39;HUAWEI&#39;:
   worksheet2.write(&#39;G&#39;+str(index[i][1]),u&#39;%s天,%s小时&#39;%(num[4],num[6]),format_rt)
  if hang > int(hang_list[2]) and hang < int(hang_list[3]):
  if num!=[]:
   if &#39;,&#39; in num[0]:
   total=num[0].replace(&#39;,&#39;,&#39;&#39;)+&#39; KB &#39;+num[3].replace(&#39;,&#39;,&#39;&#39;)+&#39; KB)&#39;
   worksheet2.write(&#39;G&#39;+str(index[i][2]),total,format_rt)
  if hang > int(hang_list[3]) and hang < int(hang_list[4]):
  if num!=[]:
   if num[0] == &#39;Memory&#39; and len(num) == 5:
   worksheet2.write(&#39;G&#39;+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] == &#39;up&#39;:
   phy+=1
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
   if num[0] == &#39;Destinations&#39; and len(num) == 10:
   worksheet2.write(&#39;G&#39;+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] == &#39;NO&#39; and num[1] == &#39;alarm&#39;:
   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(&#39;G&#39;+str(index[i][0]),u&#39;正常&#39;,format_rt) 
  else:
  worksheet2.write(&#39;G&#39;+str(index[i][0]),&#39;Error&#39;,format_red)
  worksheet2.write(&#39;G&#39;+str(index[i][3]),None,format_rt)
  worksheet2.write(&#39;G&#39;+str(index[i][5]),phy,format_rt)
  worksheet2.write(&#39;G&#39;+str(index[i][7]),u&#39;%s℃-%s℃&#39;%(temp[0],temp[-1]),format_rt)
  if alarm == 1:
  worksheet2.write(&#39;G&#39;+str(index[i][8]),u&#39;无告警&#39;,format_rt)
  else:
  worksheet2.write(&#39;G&#39;+str(index[i][8]),&#39;Error&#39;,format_red)
  worksheet2.write(&#39;G&#39;+str(index[i][9]),u&#39;正常&#39;,format_right)
except IOError as reason:
 print "11_12.py "+str(reason)
 
 
#------------------------------------------------
#------------fuzai_ip13-----------------
#------------fuzai_ip14-----------------
#------------fuzai_ip15-----------------
#------------fuzai_ip16-----------------
#------------------------------------------------
try:
 fi_list=[&#39;13DMZ-RD5412-1.txt&#39;,&#39;14DMZ-RD5412-2.txt&#39;,&#39;15SM-RD6420-1.txt&#39;,&#39;16SM-RD6420-2.txt&#39;]
 fi_name=&#39;>> &#39;
 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=&#39;/usr/sh/shell/linux/xunjian/&#39;+str(today)+&#39;/&#39;+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] == &#39;Switch&#39;:
   worksheet3.write(&#39;G&#39;+str(index[i][0]),u&#39;%s天,%s时&#39;%(num[3],num[5]),format_rt) 
  if hang > int(hang_list[1]) and hang < int(hang_list[2]):
  if num!=[]:
   if num[0] == &#39;Dual&#39;:
   if num[-1] == "OK":
   worksheet3.write(&#39;G&#39;+str(index[i][1]),u&#39;正常&#39;,format_rt)
   else:
   worksheet3.write(&#39;G&#39;+str(index[i][1]),&#39;Error&#39;,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] != &#39;Operational&#39;:
   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] == &#39;up&#39;:
   link+=1
  if hang > int(hang_list[4]) and hang < int(hang_list[5]):
  if num!=[]:
   if num[0] == &#39;Real&#39; and num[1] == &#39;Servers&#39; and len(num) == 4:
   Real_Servers=num[-1].split(&#39;(&#39;)[-1].split(&#39;)&#39;)[0]
   if num[0] == &#39;Server&#39; and num[1] == &#39;Groups&#39; and len(num) == 4:
   Server_Groups=num[-1]
   if num[0] == &#39;Virtual&#39; and num[1] == &#39;Servers&#39; and len(num) == 4:
   Virtual_Servers=num[-1].split(&#39;(&#39;)[-1].split(&#39;)&#39;)[0]
   if num[0] == &#39;Filters&#39; and len(num) == 3:
   Filters=num[-1].split(&#39;(&#39;)[-1].split(&#39;)&#39;)[0]
  if hang > int(hang_list[5]) and hang < int(hang_list[6]):
  if num!=[]:
   if num[0] == &#39;cpuUtil1Second:&#39;:
   worksheet3.write(&#39;G&#39;+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] == &#39;Total:&#39;:
   Total=int(num[-2])
   if num[0] == &#39;Free:&#39;:
   Free=int(num[-2])
  if hang > int(hang_list[7]) and hang < int(hang_list[8]):
  if num!=[]:
   if num[0] == &#39;Sensor&#39; 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] == &#39;master&#39;:
   Master+=1
   if num[-1] == &#39;backup&#39;:
   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(&#39;G&#39;+str(index[i][2]),u&#39;正常&#39;,format_rt)
  else:
  worksheet3.write(&#39;G&#39;+str(index[i][2]),&#39;Error&#39;,format_red)
  worksheet3.write(&#39;G&#39;+str(index[i][3]),link,format_rt)
  worksheet3.write(&#39;G&#39;+str(index[i][4]),&#39;%s/%s/%s/%s&#39;%(Real_Servers,Server_Groups,Virtual_Servers,Filters),format_rt)
  worksheet3.write(&#39;G&#39;+str(index[i][6]),&#39;%.2f%%&#39;%(float(Total-Free)/Total*100),format_rt)
  worksheet3.write(&#39;G&#39;+str(index[i][7]),temp[0],format_rt)
  worksheet3.write(&#39;G&#39;+str(index[i][9]),u&#39;正常&#39;,format_right)
 if state[0] == state[3]:
 worksheet3.write(&#39;G10&#39;,&#39;master&#39;,format_rt)
 worksheet3.write(&#39;G20&#39;,&#39;backup&#39;,format_rt)
 else:
 worksheet3.write(&#39;G10&#39;,&#39;Error&#39;,format_red)
 worksheet3.write(&#39;G20&#39;,&#39;Error&#39;,format_red)
 if state[4] == state[-1]:
 worksheet3.write(&#39;G30&#39;,&#39;master&#39;,format_rt)
 worksheet3.write(&#39;G40&#39;,&#39;backup&#39;,format_rt)
 else:
 worksheet3.write(&#39;G30&#39;,&#39;Error&#39;,format_red)
 worksheet3.write(&#39;G40&#39;,&#39;Error&#39;,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 &#39;+%Y%m%d&#39;) ];then
 mkdir -p $dir1/$(date &#39;+%Y%m%d&#39;)
fi
 
 
_shell()
{
pass=&#39;哈哈哈我是密码1&#39;
password=&#39;哈哈哈我是密码2&#39;
ps1=&#39;哈哈哈哈我是密码3&#39;
 
 
/usr/bin/python $dir/01DMZ-E8000E.py &#39;ip1&#39; $pass $password
/usr/bin/python $dir/02_03-M9006.py &#39;ip2&#39; $pass $password
/usr/bin/python $dir/02_03-M9006.py &#39;ip3&#39; $pass $password
/usr/bin/python $dir/04IN-FG3950B.py &#39;ip4&#39; $password
 
/usr/bin/python $dir/05_06DMZ-S9306.py &#39;ip5&#39; $pass $password
/usr/bin/python $dir/05_06DMZ-S9306.py &#39;ip6&#39; $pass $password
/usr/bin/python $dir/07_08-S12508.py &#39;ip7&#39; $pass $password
/usr/bin/python $dir/07_08-S12508.py &#39;ip8&#39; $pass $password
/usr/bin/python $dir/09_10SM-N7K.py &#39;ip9&#39; $ps1
/usr/bin/python $dir/09_10SM-N7K.py &#39;ip10&#39; $ps1
 
/usr/bin/python $dir/11_12DMZ-NE40E.py &#39;ip11&#39; $password
/usr/bin/python $dir/11_12DMZ-NE40E.py &#39;ip12&#39; $password
 
/usr/bin/python $dir/13_16-RD5412_6420.py &#39;ip13&#39; $password
/usr/bin/python $dir/13_16-RD5412_6420.py &#39;ip14&#39; $password
/usr/bin/python $dir/13_16-RD5412_6420.py &#39;ip15&#39; $password
/usr/bin/python $dir/13_16-RD5412_6420.py &#39;ip16&#39; $password
 
}
 
 
aa=`ls -l $dir1/$(date &#39;+%Y%m%d&#39;) | wc -l`
Year=$(date &#39;+%Y&#39;)
Month=$(date &#39;+%m&#39;)
Day=$(date &#39;+%d&#39;)
 
if [ $Month -lt 10 ];then
 Month=`echo $Month | sed &#39;s/0//g&#39;`
fi
 
if [ $Day -lt 10 ];then
 Day=`echo $Day | sed &#39;s/0//g&#39;`
fi
tday=$(date &#39;+%Y.%m.%d&#39;)
new_tday="$Year.$Month.$Day"
zip_tday="$Month.$Day"
 
_shell
# 这边尴尬了,输出的文件有的格式有问题,就得改改了
cp -a $dir1/$(date &#39;+%Y%m%d&#39;)/02DMZ-M9006.txt $dir1/$(date &#39;+%Y%m%d&#39;)/02DMZ-M9006.txt.bak
cat $dir1/$(date &#39;+%Y%m%d&#39;)/02DMZ-M9006.txt.bak | tr -s "\r\n" "\n" > $dir1/$(date &#39;+%Y%m%d&#39;)/02DMZ-M9006.txt
cp -a $dir1/$(date &#39;+%Y%m%d&#39;)/03SM-M9006.txt $dir1/$(date &#39;+%Y%m%d&#39;)/03SM-M9006.txt.bak
cat $dir1/$(date &#39;+%Y%m%d&#39;)/03SM-M9006.txt.bak | tr -s "\r\n" "\n" > $dir1/$(date &#39;+%Y%m%d&#39;)/03SM-M9006.txt
 
 
# 得到excel 文件
/usr/bin/python $dir/wangluo_xunjian.py > /dev/null 2>&1
 
 
if [ -f $dir1/$(date &#39;+%Y%m%d&#39;)/网络设备巡检-$tday.xlsx ];then
 sed -i &#39;s/Cmcc.*$//g&#39; $dir1/$(date &#39;+%Y%m%d&#39;)/*.txt
 zip -qj $dir1/$(date &#39;+%Y%m%d&#39;)/$zip_tday.zip $dir1/$(date &#39;+%Y%m%d&#39;)/*.txt >/dev/null
 if [ $? -ne 0 ];then
 /usr/bin/python $dir/mail_zip_error.py >/dev/null 2>&1
 else
 mv $dir1/$(date &#39;+%Y%m%d&#39;)/网络设备巡检-$tday.xlsx $dir1/$(date &#39;+%Y%m%d&#39;)/网络设备巡检-$new_tday.xlsx
 /usr/bin/python $dir/mail.py xixixi@139.com "收件人,多个以分号分隔" "$dir1/$(date &#39;+%Y%m%d&#39;)/网络设备巡检-$new_tday.xlsx" "$dir1/$(date &#39;+%Y%m%d&#39;)/$zip_tday.zip"
 fi
else
 /usr/bin/python $dir/mail_excel_error.py >/dev/null 2>&1
fi
登录后复制

6、加入定时任务

我自己定的每天上午9点

7、成品图

以上就是本篇文章的全部内容了,更多相关内容请关注PHP中文网。


以上是对Python 网络设备巡检脚本的实例讲解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板