Python Forum

Full Version: Router interface status through SNMP - suggestion required for improvement.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

This script is polling Router/Switch interfaces through SNMP, any suggestion to reduce code line, specially function calling.

#!/usr/bin/python
from pysnmp.entity.rfc3413.oneliner import cmdgen
import sys
import re
device=[]
x = "172.21.160.3"
community="readvsnl"
interface = []
int_name=[]
int_oid=[]
try:
        cmdGen = cmdgen.CommandGenerator()
        errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
            cmdgen.CommunityData(community),
            cmdgen.UdpTransportTarget((x, 161)),
            '1.3.6.1.2.1.2.2.1.2',
        )
        for varBindTableRow in varBindTable:
            for name, val in varBindTableRow:
                if 'Null' in val:  # Remove Null interface
                    continue
                elif 'Vlan' in val:  # Remove Vlan Interface
                    continue
                else:
                    int_oid.append(name)
                    int_name.append(val)
except Exception as excp:
        print("some thing went wrong")
int_admin=[]
admin=''
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((x, 161)),
        '1.3.6.1.2.1.2.2.1.7',
    )
for varBindTableRow in varBindTable:
        for name, val in varBindTableRow:
            admin_raw=val.prettyPrint()
            if '1' in admin_raw:
                admin = 'UP'
            elif '2' in admin_raw:
                admin = 'DOWN'
            elif '3' in admin_raw:
                admin = 'TESTING'
            int_admin.append(admin)
int_oper=[]
oper=''
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((x, 161)),
        '1.3.6.1.2.1.2.2.1.8',
    )
for varBindTableRow in varBindTable:
        for name, val in varBindTableRow:
            oper_raw=val.prettyPrint()
            if '1' in oper_raw:
                oper = 'UP'
            elif '2' in oper_raw:
                oper = 'DOWN'
            elif '3' in oper_raw:
                oper = 'TESTING'
            elif '4' in oper_raw:
                oper = 'UNKNOWN'
            elif '5' in oper_raw:
                oper = 'DORMANT'
            elif '6' in oper_raw:
                oper = 'NOT-PRESENT'
            elif '7' in oper_raw:
                oper = 'LOWER-LAYER-DOWN'
            int_oper.append(oper)
for i in range(len(int_name)):
    print("{}\t {}\t {}\t {}\t {}".format(x,int_oid[i],int_name[i],int_oper[i],int_admin[i]))
Output:
172.21.160.3 1.3.6.1.2.1.2.2.1.2.1 FastEthernet0/1 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.2 FastEthernet0/2 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.3 FastEthernet0/3 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.4 FastEthernet0/4 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.5 FastEthernet0/5 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.6 FastEthernet0/6 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.7 FastEthernet0/7 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.8 FastEthernet0/8 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.9 FastEthernet0/9 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.10 FastEthernet0/10 DOWN UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.11 FastEthernet0/11 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.12 FastEthernet0/12 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.13 FastEthernet0/13 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.14 FastEthernet0/14 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.15 FastEthernet0/15 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.16 FastEthernet0/16 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.17 FastEthernet0/17 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.18 FastEthernet0/18 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.19 FastEthernet0/19 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.20 FastEthernet0/20 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.21 FastEthernet0/21 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.22 FastEthernet0/22 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.23 FastEthernet0/23 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.24 FastEthernet0/24 DOWN DOWN 172.21.160.3 1.3.6.1.2.1.2.2.1.2.25 GigabitEthernet0/1 UP UP 172.21.160.3 1.3.6.1.2.1.2.2.1.2.26 GigabitEthernet0/2 DOWN UP