对Python 网络设备巡检脚本的实例讲解

本文介绍了一个使用Python编写的网络设备自动化巡检脚本,包括华为、H3C、飞塔、Cisco和Radware等设备的交互脚本,通过运行这些脚本收集数据并写入文件,然后进行数据处理和邮件通知,最终实现定时任务自动化巡检,大大提高了工作效率。
摘要由CSDN通过智能技术生成

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")
     break
    for i in range(10):
     index = child.expect(["(?i)More--","%s"%name])
     if ( index == 0 ):
     child.send(" ")
     else:
     child.sendline("exit")
     sys.exit()

11_12DMZ-NE40E.py # 华为 NE40E 交互脚本

    #!/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])
     
    child=pexpect.spawn('ssh 用户名@%s'%ip)
     
    IP=['ip1','ip2']
     
    if ip == IP[0]:
     txt='11DMZ-NE40E-1.txt'
     name='<W-SM-E11-NE40E-01>'
    elif ip == IP[1]:
     txt='12DMZ-NE40E-2.txt'
     name='<W-SM-E12-NE40E-02>'
    else:
     print "IP Input Error!"
     sys.exit()
     
    fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w')
    child.logfile = fout
    child.expect('(?i)ssword:')
    child.sendline("%s"%passwd)
    child.expect("(?i)N]:")
    child.sendline("n")
    child.expect("%s"%name)
    child.sendline("dis device | ex Normal")
    child.expect("%s"%name)
    child.sendline("dis version | in NE40E-X8 uptime")
    child.expect("%s"%name)
    child.sendline("dir")
    index = child.expect(["(?i)---- More ----","%s"%name])
    if ( index == 0 ):
     child.send(" ")
     index = child.expect(["(?i)---- More ----","%s"%name])
     if ( index == 0 ):
     child.send(" ")
     else:
     child.sendline("dis memory-usage")
    else:
     child.sendline("dis memory-usage")
    child.expect("%s"%name)
    child.sendline("dis int brief | in up")
    child.expect("%s"%name)
    child.sendline("dis ip routing-table protocol static")
    child.expect("%s"%name)
    child.sendline("dis temperature")
    for i in range(40):
     index = child.expect(["(?i)---- More ----","%s"%name])
     if ( index == 0 ):
     child.send(" ")
     else:
     child.sendline("dis alarm all")
     break
    child.expect("%s"%name)
    child.sendline("dis logbuffer | in %s"%tdy)
    for i in range(30):
     index = child.expect(["(?i)---- More ----","%s"%name])
     if ( index == 0 ):
     child.send(" ")
     else:
     child.sendline("q")
     break

13_16-RD5412_6420.py # Radware RD5412 交互脚本,只要掌握一个,其他都是套路

    #!/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])
     
    child=pexpect.spawn('ssh 还是用户名@%s'%ip)
     
    IP=['ip1','ip2','ip3','ip4']
     
    if ip == IP[0]:
     txt='13DMZ-RD5412-1.txt'
    elif ip == IP[1]:
     txt='14DMZ-RD5412-2.txt'
    elif ip == IP[2]:
     txt='15SM-RD6420-1.txt'
    elif ip == IP[3]:
     txt='16SM-RD6420-2.txt'
    else:
     print "IP Input Error!"
     sys.exit()
    name='Standalone ADC(?i)'
     
    fout=file('/usr/sh/shell/linux/xunjian/'+today+'/'+txt,'w')
    child.logfile = fout
    child.expect('(?i)ssword:')
    child.sendline("%s"%passwd)
    if ip == IP[3]:
     child.expect("(?i)y]:")
     child.sendline("y")
    child.expect("%s"%name)
    child.sendline("/i/sys/general")
    child.expect("%s"%name)
    child.sendline("/i/sys/ps")
    child.expect("%s"%name)
    child.sendline("/i/sys/fan")
    child.expect("%s"%name)
    child.sendline("/i/link")
    index = child.expect(["Press q to quit, any other key to continue","%s"%name])
    if ( index == 0 ):
     child.send
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值