Search code examples
python-3.xsnmppysnmp

How to walk sub table with pysnmp


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)

Solution

  • 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.