重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
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