Python port monitoring example

Source: Internet
Author: User

Recently, due to work requirements, the above requirements need to be reported daily O & M of hundreds of port usage [], although there is a ready-made network management monitoring tool to monitor the device status, but reporting is a little inconvenient, close up a small script.

Note: snmpwalk must be installed to test running on ipvtn,

Related snmp configuration is required for the target DT-pass device.

Copy codeThe Code is as follows:
#/Usr/bin/python
# Coding: UTF-8

Import OS, sys
Import re
From pprint import pprint

# Why?
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 ':{
}
}
# Why?
Def portStatus (_ s ):
If int (_ s) = 2:
Return 'drop'
Elif int (_ s) = 1:
Return 'up'
Else:
Return 'none'

# Skip skip snmpwalk
Def snmpwalk (host, publicKey, iso ):
Return [I. strip () for I in OS. popen ('/usr/bin/snmpwalk-c % s-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'] ['Port'] ['string'])
PostsStatus = (int (j. split ('=') [1]. split (':') [1]. strip () for j in snmpwalk (_ ip, _ public, MIB ['public'] ['Port'] ['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': publicKey },
'10. 221.98.3 ': {'type': 'switch', 'origin': 'quidway', 'public': publicKey },
'10. 221.97.108 ': {'type': 'firewall', 'origin': 'h3c', 'public': publicKey },
'10. 231.98.233 ': {'type': 'switch', 'origin': 'cisco', 'public': publicKey },
}
If _ name _ = '_ main __':
For I in HOSTS. keys ():
For host, status in getPorts (I, HOSTS [I] ['public'], 'Port '):
Print ("% s \ t % s" % (I, host. ljust (30), portStatus (status). ljust (20 )))
Print (''. ljust (50 ,'#'))

Running result
Copy codeThe Code is as follows:
Root @ override tn12:/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.