Python implements a digital device port usage monitoring instance

Source: Internet
Author: User
Tags snmpwalk pprint
The example in this paper describes how Python implements monitoring of port usage for several-pass devices. Share to everyone for your reference. Specific as follows:

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

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

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

#/usr/bin/python#coding:utf-8import os,sysimport refrom pprint import pprint# ning song  hyo indent companion? 氳 澶 嘙 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}}, ' Huawei ' : {}, ' Cisco ': {}} #绔彛鐘舵? Bauhinia paste 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 the Commmnad ') PublicKey = ' Hi ' #sunmp public keyhosts={ ' 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 ()) print (". Ljust (50, ' # '))

Operation Result:

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

Hopefully this article will help you with Python programming.

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