1、基本信息
我公司之前采用的是人工巡检,但奈何有大量网络设备,往往巡检需要花掉一上午(还是手速快的话),浪费时间浪费生命。
这段时间正好在学 Python ,于是乎想(其)要(实)解(就)放(是)双(懒)手。
好了,脚本很长又比较挫,有耐心就看看吧。
需要巡检的设备如下:
设备清单 | 设备型号 |
防火墙 | 华为 E8000E |
H3C M9006 | |
飞塔 FG3950B | |
交换机 | 华为 S9306 |
H3C S12508 | |
Cisco N7K | |
路由器 | 华为 NE40E |
负载 | Radware RD5412 |
Radware RD6420 |
2、采集数据并写入文件
# 相关设备巡检命令就不介绍了。。。
01DMZ-E8000E.py # 华为 E8000E 交互脚本
#!/usr/bin/env pythonimport pexpectimport sysimport 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 = foutchild.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
新闻热点
疑难解答