Python PySNMP构建器无法加载CISCO-RTTMON-MIB

Python PySNMP构建器无法加载CISCO-RTTMON-MIB,python,snmp,pysnmp,Python,Snmp,Pysnmp,在尝试加载CISCO-RTTMON-MIB时,我在PySNMP构建器中不断遇到此错误。下面的代码适用于我迄今为止尝试过的所有其他MIB,但这一个被卡住了。这也是我第一次尝试遍历整个表(rttMonStats),所以我可能只是做错了。以下是我所做的: 我下载了版本2列下的所有文件: 标记为“非Cisco MIB”的文件,我在网上的其他地方找到了“下载MIB_名称”。我通过build pysnmp MIB运行了这些文件,如: build-pysnmp-mib MIB_NAME.my > MIB

在尝试加载CISCO-RTTMON-MIB时,我在PySNMP构建器中不断遇到此错误。下面的代码适用于我迄今为止尝试过的所有其他MIB,但这一个被卡住了。这也是我第一次尝试遍历整个表(rttMonStats),所以我可能只是做错了。以下是我所做的:

我下载了版本2列下的所有文件:

标记为“非Cisco MIB”的文件,我在网上的其他地方找到了“下载MIB_名称”。我通过build pysnmp MIB运行了这些文件,如:

build-pysnmp-mib MIB_NAME.my > MIB_NAME.py.
然后我将所有*.py文件复制到/opt/appname/mib中/

以下是snmpcommands.py中的相关定义:

def walk(community, ipaddress, mib, oid, index):
    cmdGen = cmdgen.CommandGenerator()
    mibBuilder = cmdGen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
    mibSources = mibBuilder.getMibSources() + (
            builder.DirMibSource('/opt/appname/mibs'),
            )
    mibBuilder.setMibSources(*mibSources)
    mibBuilder.loadModules()
    errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
            cmdgen.CommunityData(community, mpModel=0),
            cmdgen.UdpTransportTarget((ipaddress, 161)),
            cmdgen.MibVariable(mib, oid, index),
            lookupValues=True, lookupNames=True
    )

    if errorIndication:
            print(errorIndication)
    else:
            if errorStatus:
                    print('%s at %s' % (
                            errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex)-1] or '?'
                            )
                    )
            else:
                    result = {}
                    for tableRow in varBindTable:
                            for oid, val in tableRow:
                                    result[oid.prettyPrint()] = val.prettyPrint()

                    return json.dumps(result)
我这样称呼它:

>>>import snmpcommands
>>>snmpcommands.walk('community', 'ip.add.ress', 'CISCO-RTTMON-MIB', 'rttMonStats', '0.0.0.0')
然而,我明白了:

>>> snmpcommands.walk('community', 'ip.add.ress', 'CISCO-RTTMON-MIB', 'rttMonStats', '0.0.0.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "snmpcommands.py", line 57, in walk
    mibBuilder.loadModules()
  File "/usr/lib/python2.6/site-packages/pysnmp-4.2.4-py2.6.egg/pysnmp/smi/builder.py", line 251, in loadModules
    'MIB module \"%s\" load error: %s' % (modPath, sys.exc_info()[1])
pysnmp.smi.error.SmiError: MIB module "/opt/appname/mibs/CISCO-RTTMON-MIB.py" load     error:     ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(), ValueSizeConstraint(0, 65535)), ValueSizeConstraint(0, 255)), ValueSizeConstraint(1, 64)) failed at: "ValueSizeConstraint(1, 64) failed at: """ at SnmpAdminString
snmpcommands.walk('community','ip.add.ress','CISCO-RTTMON-MIB','rttMonStats','0.0.0') 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“snmpcommands.py”,第57行,在walk中 mibBuilder.loadModules() loadModules中的文件“/usr/lib/python2.6/site packages/pysnmp-4.2.4-py2.6.egg/pysnmp/smi/builder.py”,第251行 “MIB模块\%s\”加载错误:%s%”(modPath,sys.exc_info()[1]) pysnmp.smi.error.smiror:MIB module“/opt/appname/mibs/CISCO-RTTMON-MIB.py”加载错误:ConstraintIntersection(ConstraintIntersection)(ConstraintIntersection(ConstraintIntersection(),ValueSizeConstraint(0,65535)),ValueSizeConstraint(0,255)),ValueSizeConstraint(1,64))在以下位置失败:“在SnmpAdminString
我对PySNMP非常陌生,所以我猜问题在于它希望在snmpadmin中有一个值,它从SNMP-FRAMEWORK-MIB中提取,而该值为空。我只是不知道如何修复它。

看起来您在CISCO-RTTMON-MIB.py上有一个空字符串作为SnmpAdminString(“”)初始值设定项。这似乎违反了SNMPAdministring约束,最终导致了异常。因此,我建议grep CISCO-RTTMON-MIB.py查找空的snmpadmin初始值设定项,并用符合要求的值(1-64个八位字节)替换它们,或者干脆删除空的初始值设定项(例如,使其看起来像snmpadmin())。

谢谢您的回答!我的问题是,他们重新配置了我要查询的设备,它停止了响应。空字符串是因为缺少响应。结果证明您是对的。我发现了几个SnmpAdminString.clone(“”)的实例,当问题重新引入到我的项目中时,我将它们替换为SnmpAdminString.clone(“”)。