我一直在尝试实现加载设备mib并遍历所有oid的代码。在这种情况下,当我尝试加载snmp 1.3.6.1.2.1.11的OID时,smi在尝试加载特定的OID时抛出异常。前一个OID工作成功:'.1.3.6.1.2.1.11.29.0',但此OID生成错误消息'.1.3.6.1.2.1.11.30.0'
例外情况是:
File "/opt/anaconda/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 859, in resolveWithMib raise SmiError('MIB object %r having type %r failed to cast value %r: %s' % (self.args[0].prettyPrint(), self.__args[0].getMibNode().getSyntax().__class.name, self.__args[1], sys.exc_info()[1])) ;SmiError: MIB object 'SNMPv2-MIB::snmpEnableAuthenTraps.0' having type 'Integer32' failed to cast value Integer32(808466736): ConstraintsIntersection(ConstraintsIntersection(ConstraintsIntersection(), ValueRangeConstraint(-2147483648, 2147483647)), SingleValueConstraint(1, 2)) failed at: "SingleValueConstraint(1, 2) failed at: "808466736"" at Integer32
下面是演示错误的示例代码。您需要修改设备的IP。假设您运行的是snmpv1并针对社区的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')
您的SNMP代理的响应是1.3.6.1.2.1.11.30.0=808466736,而OID 1.3.6.1.1.11.30.0只允许使用两个值标识INTEGER类型的MIB objectsnmpenableuthentraps。在
以下是来自SNMPv2 MIB的正式定义:
所以这一次pysnmp似乎做了正确的事情——它保护您不受毫无意义的价值的影响。此问题的根本原因是SNMP代理程序为MIB对象发送格式错误的值。在
相关问题 更多 >
编程相关推荐