Python Forum
Router interface status through SNMP - suggestion required for improvement.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Router interface status through SNMP - suggestion required for improvement.
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,625 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  Receiving snmp traps with more than one Community String ilknurg 0 2,234 Jan-19-2022, 09:02 AM
Last Post: ilknurg
  Can you give me some suggestion about PCEP Newbie1114 0 1,028 Oct-14-2021, 03:02 PM
Last Post: Newbie1114
  instagram followers name without suggestion for you jacklee26 1 3,172 Oct-02-2021, 04:57 AM
Last Post: ndc85430
  Random coordinate generator speed improvement saidc 0 2,060 Aug-01-2021, 11:09 PM
Last Post: saidc
  Function Improvement Santino 1 1,813 May-23-2020, 03:30 PM
Last Post: jefsummers
  Name Mashup Program Improvement in Python rhat398 3 2,571 Apr-05-2020, 12:09 PM
Last Post: perfringo
  Optimization suggestion Julia 2 1,738 Mar-29-2020, 12:02 PM
Last Post: Julia
  Need suggestion how to handle this error farrukh 1 2,271 Dec-21-2019, 03:21 PM
Last Post: DeaD_EyE
  first try with python class, suggestion for improvement please anna 18 5,953 Nov-01-2019, 11:16 AM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020