新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

对Python网络设备巡检脚本的实例讲解-创新互联

1、基本信息

泉港ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

我公司之前采用的是人工巡检,但奈何有大量网络设备,往往巡检需要花掉一上午(还是手速快的话),浪费时间浪费生命。

这段时间正好在学 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

分享文章:对Python网络设备巡检脚本的实例讲解-创新互联
文章出自:http://www.wjwzjz.com/article/doppog.html
在线咨询
服务热线
服务热线:028-86922220
TOP