mitch2k
mitch2k

Reputation: 526

Get a single SNMP value with pysnmp

I'm trying to get the value of a single OID into a variable. However, I only am able to walk it using pysnmp. I want to get the value of OID 1.3.6.1.4.1.2.3.51.2.2.7.1.0 which returns value 255 if I test it using an SNMP tool.

Using this code I don't get any output:

def getsnmp(host, oid):

    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData('public', mpModel=0),
                              UdpTransportTarget((host, 161)),
                              ContextData(),
                              ObjectType(ObjectIdentity(oid)),
                              lookupMib=False,
                              lexicographicMode=False):

        if errorIndication:
            print(errorIndication)
            break

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

        else:
            for varBind in varBinds:
                print(' = '.join([x.prettyPrint() for x in varBind]))

getsnmp('10.100.11.30', '1.3.6.1.4.1.2.3.51.2.2.7.1.0')

However, If I remove the last .0, I get as a result:

1.3.6.1.4.1.2.3.51.2.2.7.1.0 = 255

How do I access the specific OID directly without a walk?

Thanks!

Upvotes: 2

Views: 2899

Answers (1)

Ilya Etingof
Ilya Etingof

Reputation: 5555

The nextCmd returns the next OID relative to the given one. It never returns the value for the given OID.

If you need to query specific OID you should use getCmd function instead of nextCmd.

Upvotes: 3

Related Questions