简体   繁体   中英

How to get correct MIB for sysObjectID in pysnmp

When I have all the necessary MIB files compiled and loaded to pysnmp, the sysObjectID should return a fully parsed MIB. But it doesn't

What I've done so far is, I created an mib_builder and added precompiled mib source to the builder, passed the mib builder to SnmpEngine from hlapi through MsgAndPduDispatcher providing MibInstrumController. And then requested for sysObjectID.

Consider the following block of code:

from pysnmp.smi import builder, view, compiler, error, instrum
from pysnmp.proto.rfc3412 import MsgAndPduDispatcher
from pysnmp.hlapi import *

mib_builder = builder.MibBuilder()
mib_builder.addMibSources(builder.DirMibSource('/path/to/compiled/mibs/'))
engine = SnmpEngine(msgAndPduDsp=MsgAndPduDispatcher(mibInstrumController=instrum.MibInstrumController(mib_builder)))

oid = ObjectIdentity("SNMPv2-MIB", "sysObjectID")
for (errorIndication,
     errorStatus,
     errorIndex,
     varBinds) in nextCmd(
        self.engine,
        CommunityData('public', mpModel=1),
        UdpTransportTarget(('192.168.0.222', 161)),
        ContextData(),
        ObjectType(oid),
        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(varBind)

It should return correct MIB for sysObjectID. But it returns,

SNMPv2-MIB::sysObjectID.0 = SNMPv2-SMI::enterprises.9.1.1047

then I tried,

for varBind in varBinds:
   print(varBind)
   if type(varBind[1]) == type(oid):
       _oid, label, suffix = view.MibViewController(mib_builder).getNodeName(varBind[1].getOid())
       print(_oid, label, suffix)

which returns

SNMPv2-MIB::sysObjectID.0 = SNMPv2-SMI::enterprises.9.1.1047
1.3.6.1.4.1 ('iso', 'org', 'dod', 'internet', 'private', 'enterprises') 9.1.1047

The correct MIB resides in CISCO-PRODUCTS-MIB and I have it compiled.

So, what can I do to get correct MIB for sysObjectID?

TL;DR; - try adding .loadMibs('CISCO-PRODUCTS-MIB') to your ObjectIdentity object.

The reason why you might need that is that pysnmp does not map OIDs to MIBs automatically. So when pysnmp gets an OID to translate, it only tries that with the MIBs it has loaded already.

BTW, you do not need so much code to achieve what you are trying to. Just a base line SNMP get/walk (plus .loadMibs() for the MIBs you anticipate to work with) should suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM