0

I would like to walk the snmp table under enterprises.35604.2.3.5.7.2 oid. This will return a list of logs.

When I execute the code it does not stop after the last table element. How can I make sure it will stop after all the sub table done?

from pysnmp.hlapi import *
logoid='1.3.6.1.2.1.69.1.5.8.1.7'


def scan_cm_log(ipaddress, oid):
    for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(SnmpEngine(),
               CommunityData('<key>'),
               UdpTransportTarget((ipaddress, 161)),
               ContextData(),
               ObjectType(ObjectIdentity(oid)),
               ):

        if not errorIndication and not errorStatus:
            for varBind in varBinds:
                result=' = '.join([x.prettyPrint() for x in varBind])
                print(result)


scan_cm_log('<ip>', logoid)
Krisz
  • 701
  • 7
  • 23

2 Answers2

2

Try adding lexicographicMode=False flag as explained here.

iterator = nextCmd(
    SnmpEngine(),
    CommunityData('<key>'),
    UdpTransportTarget((ipaddress, 161)),
    ContextData(),
    ObjectType(ObjectIdentity(oid)),
    lexicographicMode=False)

for (errorIndication, errorStatus, errorIndex, varBinds) in iterator:
    ...

That should exhaust the iterator once all of the OIDs you query leave their respective initial OID prefixes.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
0

Examine varBind to see where you're up to and do a return if you've left the table.

Do this by ending the walk when you're getting responses for OIDs for which your original OID is not a prefix.

So, if you're walking 1.2.3.4, and you get 1.2.3.4.1/1.2.3.4.2/1.2.3.4.3/1.2.3.5.1/1.2.3.5.2 back, "1.2.3.5.1" does not start with "1.2.3.4" so you're done at that point.

Ultimately it's completely up to you as to what you want to do, and with what logic/algorithm. But the above is a "typical" walk algorithm.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055