Python implements the datastore device port monitoring example

  • 2020-04-02 13:34:37
  • OfStack

Recently, due to the work needs, the above requirements, every day to report the operation and maintenance of several hundred through equipment port use situation [], although there is a ready-made network management monitoring tools to monitor the status of equipment, but the report, a little inconvenient, close-up of a small script.

Note: the test runs on ubuntn. Snmpwalk tool shall be installed.

The target digital device needs to be configured with SNMP


#/usr/bin/python
#coding:utf-8
import os,sys
import re
from pprint import pprint

# I can enjoy the music in my own way MIB � ?
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':{
   }
}
# A dandy has a rudderand a gill 
def portStatus(_s):
    if int(_s)==2:
        return 'down'
    elif int(_s)==1:
        return 'up'
    else:
        return 'none'

#  �  ц  �  Fei meat  �   �  to quit snmpwalk Zhang  �   �  
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']['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':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("%st%st%s"%(i,host.ljust(30),portStatus(status).ljust(20)))
        print(''.ljust(50,'#'))

The results


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     


Related articles: