Python port monitoring example

Source: Internet
Author: User
Tags snmpwalk pprint
This article mainly introduces the example of how to monitor the port of a digital communication device using python. if you need it, refer to the preceding requirements for recent work, report the port usage of hundreds of connected devices required for O & M every day []. although there is a ready-made network management monitoring tool to monitor the device status, it is inconvenient to make a report and 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.

The code is as follows:


#/Usr/bin/python
# Coding: UTF-8

Import OS, sys
Import re
From pprint import pprint

# Are you a companion? Which of the following statements are supported?
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 ':{
}
}
# Have you ever taken the helm? Master?
Def portStatus (_ s ):
If int (_ s) = 2:
Return 'drop'
Elif int (_ s) = 1:
Return 'Up'
Else:
Return 'none'

# 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

The 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.