Python implementation of digital device port monitoring sample _python

Source: Internet
Author: User
Tags snmpwalk pprint

Recently due to work needs, the above requirements, daily need to report operational dimensions of the hundreds of-pass device port usage "", although there is a ready-made network management monitoring tools monitoring equipment status, but do a report, a little inconvenient, close-up a small script.

Note: The test runs in Ubuntn, the Snmpwalk tool must be installed,

Target number-pass equipment, need to do the relevant SNMP configuration

Copy Code code as follows:

#/usr/bin/python
#coding: Utf-8

Import Os,sys
Import re
From Pprint import Pprint

#甯歌  鍘 傚  鏁 氳,  and weather ib necklace?
MIB = {
' Public ': {
' Ports ': {
' String ': '. 1.3.6.1.2.1.2.2.1.2 ',
' Status ': '. 1.3.6.1.2.1.2.2.1.8 ', #2 down
}
},
' The ' of the ": {

  },
   ' Cisco ': {
  }
}
#绔  Hani clock rudder? 佽 slurry 鎹?
def portstatus (_s):
    if int (_s) ==2:
        return ' Down '
    elif int (_s) ==1:
        return ' up '
     else:
        return ' None '

#鎵ц  Fei forgings 粺 snmpwalk Zhang 冪 敤
def snmpwalk (host,publickey,iso):
    return [I.strip () to I in Os.popen ('/usr/bin/snmpwalk-c%s& nbsp -V 2c%s%s '% (Publickey,host,iso)). ReadLines ()]

def getports (_ip,_public,option):
if option = = ' ports ':
Postsstring = (j.split (' = ') [1].split (': ') [1].replace (' ", ']"). Strip () for J in Snmpwalk (_ip,_public,mib[' public '] Ports ' [' String '])
Postsstatus = (int (j.split (' = ') [1].split (': ') [1].strip ()) for J. Snmpwalk (_ip,_public,mib[' public '] [' ports '] ['] Status '])
return Zip (postsstring,postsstatus)
Else
Print (' On this Commmnad ')

PublicKey = ' Hi ' #sunmp public key

hosts={
' 10.221.98.2 ': {' type ': ' Switch ', ' origin ': ' Quidway ', ' Public ':p Ublickey},
' 10.221.98.3 ': {' type ': ' Switch ', ' origin ': ' Quidway ', ' Public ':p Ublickey},
' 10.221.97.108 ': {' type ': ' Firewall ', ' origin ': ' h3c ', ' Public ':p Ublickey},
' 10.231.98.233 ': {' type ': ' Switch ', ' Origin ': ' Cisco ', ' Public ':p Ublickey},
}
if __name__ = = ' __main__ ':
For I in Hosts.keys ():
For Host,status in Getports (i,hosts[i][' public ', ' ports '):
Print ("%s\t%s\t%s"% (I,host.ljust), Portstatus (status). Ljust (20))
Print ('. Ljust (50, ' # '))

Run results

Copy Code code as follows:

root@ubuntn12:/tmp# python snmpwalk.py
10.221.98.2 InLoopBack0 up
10.221.98.2 NULL0 up
10.221.98.2 gigabitethernet0/0/0 Down
10.221.98.2 AUX0/0/1 Down
10.221.98.2 Vlanif100 up
10.221.98.2 Eth-trunk1 Down
10.221.98.2 eth-trunk1.32 Down
10.221.98.2 eth-trunk1.50 Down
10.221.98.2 eth-trunk1.60 Down
10.221.98.2 eth-trunk1.70 Down
10.221.98.2 eth-trunk1.80 Down
10.221.98.2 eth-trunk1.100 Down
10.221.98.2 eth-trunk1.110 Down

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.