没有合适的资源?快使用搜索试试~ 我知道了~
首页对Python 网络设备巡检脚本的实例讲解
1、基本信息 我公司之前采用的是人工巡检,但奈何有大量网络设备,往往巡检需要花掉一上午(还是手速快的话),浪费时间浪费生命。 这段时间正好在学 Python ,于是乎想(其)要(实)解(就)放(是)双(懒)手。 好了,脚本很长又比较挫,有耐心就看看吧。 需要巡检的设备如下: 设备清单 设备型号 防火墙 华为 E8000E H3C M9006 飞塔 FG3950B 交换机 华为 S9306 H3C S12508 Cisco N7K 路由器 华为 NE40E 负载 Radware RD541
资源详情
资源评论
资源推荐
对对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
剩余32页未读,继续阅读
weixin_38715831
- 粉丝: 4
- 资源: 991
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 2023年中国辣条食品行业创新及消费需求洞察报告.pptx
- 2023年半导体行业20强品牌.pptx
- 2023年全球电力行业评论.pptx
- 2023年全球网络安全现状-劳动力资源和网络运营的全球发展新态势.pptx
- 毕业设计-基于单片机的液体密度检测系统设计.doc
- 家用清扫机器人设计.doc
- 基于VB+数据库SQL的教师信息管理系统设计与实现 计算机专业设计范文模板参考资料.pdf
- 官塘驿林场林防火(资源监管)“空天地人”四位一体监测系统方案.doc
- 基于专利语义表征的技术预见方法及其应用.docx
- 浅谈电子商务的现状及发展趋势学习总结.doc
- 基于单片机的智能仓库温湿度控制系统 (2).pdf
- 基于SSM框架知识产权管理系统 (2).pdf
- 9年终工作总结新年计划PPT模板.pptx
- Hytera海能达CH04L01 说明书.pdf
- 数据中心运维操作标准及流程.pdf
- 报告模板 -成本分析与报告培训之三.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0