Python to monitor the port usage of the device, python to monitor the port usage of the device

Source: Internet
Author: User
Tags snmpwalk pprint

Python to monitor the port usage of the device, python to monitor the port usage of the device

This example describes how to monitor the port usage of a device using Python. Share it with you for your reference. The details are as follows:

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. Uploaded, available for semi-finished products

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

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

#/Usr/bin/python # coding: utf-8import OS, sysimport refrom 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 ':{}# € €€? Def portStatus (_ s): if int (_ s) = 2: return 'drop' elif int (_ s) = 1: return 'up' else: return 'none' # Skip skip 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 keyHOSTS = {'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'], 'ports '): print ("% s \ t % s" % (I, host. ljust (30), portStatus (status ). ljust (20) print (''. ljust (50 ,'#'))

Running result:

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

I hope this article will help you with Python programming.

Related Article

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.