Python implements a digital device port monitoring example

Source: Internet
Author: User
Tags snmpwalk pprint
Recently due to work needs, the above requirements, the daily need to report operation and maintenance of the hundreds of number of devices port usage "", although there are ready-made network management monitoring tools to monitor the status of the device, but do report, a little inconvenient, close up a small script.

Note: The test runs on UBUNTN, you need to install the Snmpwalk tool,

Target number of devices, need to do the relevant SNMP configuration

Copy the Code code as follows:


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

Import Os,sys
Import re
From Pprint import Pprint

#甯歌鍘傚鏁伴? 氳 澶 嘙 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
}
},
' Huawei ': {

},
' Cisco ': {
}
}
#绔彛鐘舵? Bauhinia pulp is?
def portstatus (_s):
if int (_s) ==2:
Return ' Down '
elif Int (_s) ==1:
Return ' Up '
Else
Return ' None '

#鎵ц绯荤粺鍛戒护snmpwalk璋冪敤
def snmpwalk (Host,publickey,iso):
return [I.strip () for I in Os.popen ('/usr/bin/snmpwalk-c%s-v 2c%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 in 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 the 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.