【发布时间】:2016-08-03 17:52:23
【问题描述】:
我一直在尝试实现加载我的设备 MIBS 并遍历所有 OIDS 的代码。在这种情况下,当我尝试为 snmp 1.3.6.1.2.1.11 加载 OID 时,smi 在尝试加载特定 OID 时会引发异常。前一个 OID 工作成功:'.1.3.6.1.2.1.11.29.0' 但这个会生成错误消息 '.1.3.6.1.2.1.11.30.0'
例外是:
文件“/opt/anaconda/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py”,第 859 行,在 resolveWithMib raise SmiError('MIB object %r with type %r failed to cast value %r: %s' % (self.args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax ().__class.名称, self.__args[1], sys.exc_info()[1])) ;SmiError: MIB 对象 'SNMPv2-MIB::snmpEnableAuthenTraps.0' 类型为 'Integer32' 无法转换值 Integer32(808466736): ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(), ValueRangeConstraint(-2147483648, 2147483647)), SingleValueConstraint(1, 2)) 在 Integer32 处失败:“SingleValueConstraint(1, 2) 在:“808466736”处失败
这里是演示错误的示例代码。您将需要修改 DEVICE_IP。假设您正在运行 SNMP v1 并且针对社区 'public.它正在运行 pysnmp 版本 4.3.2
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi.rfc1902 import ObjectIdentity
DEVICE_IP = 'localhost'
def get_oid(oid):
"""
Requires a valid oid as input and retrieves the given OID
"""
snmp_target = (DEVICE_IP, 161)
cmdGen = cmdgen.CommandGenerator()
result = None
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData('public', mpModel=0),
cmdgen.UdpTransportTarget(snmp_target),
ObjectIdentity(oid, last=True),
lexicographicMode=False
)
if errorIndication:
print(errorIndication)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
try:
result = str(val)
except:
raise
return result
# Does not Throw Error
print get_oid('.1.3.6.1.2.1.11.29.0')
# Throws Error
print get_oid('.1.3.6.1.2.1.11.30.0')
【问题讨论】: