Search code examples
pythonpython-2.7snmppysnmp

pysnmp get subtree and do not resolve oid against mib name


I want to provide pysnmp with a base oid for example 1.3.6.1.2.1.2.2.1.8 and be able to get all it's children underneath it e.g 1.3.6.1.2.1.2.2.1.8.1 - 1.3.6.1.2.1.2.2.1.8.n without going over to 1.3.6.1.2.1.2.2.1.9. I was wondering how I would go about this? I have also tried doing a GETNEXT but not sure how to go about achieving what I want. Another question is pysnmp seems to try to resolve the oid to the mib associated with it, how do I go about turning this feature off? I am currently using the latest version of pysnmp.


Solution

  • Try passing both lookupMib=False and lexicographicMode=False to nextCmd() or bulkCmd():

    from pysnmp.hlapi import *
    
    for errorIndication, errorStatus, \
        errorIndex, varBinds in bulkCmd(
            SnmpEngine(),
            CommunityData('public'),
            UdpTransportTarget(('demo.snmplabs.com', 161)),
            ContextData(),
            0, 50,  # GETBULK specific: request up to 50 OIDs in a single response
            ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.8')),
            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]))