简体   繁体   English

如何在pysnmp中获取sysObjectID的正确MIB

[英]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. 当我将所有必需的MIB文件编译并加载到pysnmp时,sysObjectID应该返回一个完全解析的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. 到目前为止我做的是,我创建了一个mib_builder并将预编译的mib源添加到构建器,通过提供MibInstrumController的MsgAndPduDispatcher将mib构建器从hlapi传递给SnmpEngine。 And then requested for sysObjectID. 然后请求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. 它应该为sysObjectID返回正确的MIB。 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. 正确的MIB驻留在CISCO-PRODUCTS-MIB中,我编译了它。

So, what can I do to get correct MIB for sysObjectID? 那么,我该怎么做才能为sysObjectID获取正确的MIB?

TL;DR; TL; DR; - try adding .loadMibs('CISCO-PRODUCTS-MIB') to your ObjectIdentity object. - 尝试将.loadMibs('CISCO-PRODUCTS-MIB')添加ObjectIdentity对象。

The reason why you might need that is that pysnmp does not map OIDs to MIBs automatically. 您可能需要的原因是pysnmp不会自动将OID映射到MIB。 So when pysnmp gets an OID to translate, it only tries that with the MIBs it has loaded already. 因此,当pysnmp获得要翻译的OID时,它只会尝试使用已加载的MIB。

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. 只需一条基线SNMP get / walk (加上你期望使用的MIB的.loadMibs() )就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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