没有合适的资源?快使用搜索试试~ 我知道了~
对Python 网络设备巡检脚本的实例讲解
16 下载量 50 浏览量
2020-09-20
14:15:33
上传
评论 1
收藏 313KB PDF 举报
温馨提示


试读
18页
下面小编就为大家分享一篇对Python 网络设备巡检脚本的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
资源推荐
资源详情
资源评论









对对Python 网络设备巡检脚本的实例讲解网络设备巡检脚本的实例讲解
下面小编就为大家分享一篇对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")
剩余17页未读,继续阅读
资源评论


weixin_38677046
- 粉丝: 6
- 资源: 912
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 学生成绩管理系统(C语言).txt
- 学生成绩管理系统(C语言).txt
- Screenshot_2023-12-10-12-52-34-127_net.csdn.csdnplus.jpg
- Screenshot_2023-12-10-12-50-49-374_net.csdn.csdnplus.jpg
- mxnet-cu111-1.9.0b20230903-py3-none-manylinux2014-x86-64.whl
- mxnet-cu110-2.0.0b20220324-py3-none-manylinux2014-x86-64.whl
- mxnet-cu110-1.9.1b20220823-py3-none-manylinux2014-x86-64.whl
- mxnet-cu110-1.9.0-py3-none-manylinux2014-x86-64.whl
- mxnet-cu110-1.9.0b20230903-py3-none-manylinux2014-x86-64.whl
- mxnet-cu110-1.8.0b20210324-py2.py3-none-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
