pythonpython-3.xsnmppysnmp

How to get the value of OID in Python using PySnmp


Using snmpwalk I can get this from my device:

OID=.1.3.6.1.4.1.5296.1.9.1.1.1.7.115.101.99.99.97.57.27.1.41
Type=OctetString
Value=secca99

I tried this program in Python to get the value field from above OID:

#!/usr/bin/env python3

from pysnmp.hlapi import *
import sys

def walk(host, oid):

    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData('public'),
                              UdpTransportTarget((host, 161)),
                              ContextData(),
                              ObjectType(ObjectIdentity(oid))):

        if errorIndication:
            print(errorIndication, file=sys.stderr)
            break

        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'), file=sys.stderr)
            break

        else:
            for varBind in varBinds:
                print(varBind)


walk('10.78.163.39',
     '.1.3.6.1.4.1.5296.1.9.1.1.1.7.115.101.99.99.97.57.27.1.41')

Output i get:

When i run the program, it shows a long list of OID's (don't know why even I am giving the leaf level OID as input in program) with values. STRANGE.

What is tried

lexicographicMode=True in the nextCmd but it doesn't show anything than.

What i wish

I want to give a list of OID in my program and wants their values(value is a key you can see in first line), that's it.

Request

Please help me in python program to do so using pysnmp.


Solution

  • If you want OIDs, use mibLookup=False parameter. If you want just the branch of the MIB, use lexicographicMode=False, but make sure to specify non-leaf OID because in that case you will get nothing in return.

    Here's your script with the suggested changes:

    from pysnmp.hlapi import *
    import sys
    
    def walk(host, oid):
    
        for (errorIndication,
             errorStatus,
             errorIndex,
             varBinds) in nextCmd(SnmpEngine(),
                                  CommunityData('public'),
                                  UdpTransportTarget((host, 161)),
                                  ContextData(),
                                  ObjectType(ObjectIdentity(oid)),
                                  lookupMib=False,
                                  lexicographicMode=False):
    
            if errorIndication:
                print(errorIndication, file=sys.stderr)
                break
    
            elif errorStatus:
                print('%s at %s' % (errorStatus.prettyPrint(),
                                    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'), file=sys.stderr)
                break
    
            else:
                for varBind in varBinds:
                     print('%s = %s' % varBind)
    
    walk('demo.snmplabs.com', '1.3.6.1.2.1.1.9.1.2')
    

    You should be able to cut&paste it, it's running against public SNMP simulator at demo.pysnmp.com.