我有问题pysnmp库。我试图从我的路由器SNMP读取信息。Snmpwal工作正常,没有问题…
from pysnmp.hlapi import *
router_ip = '192.168.88.254'
community = 'public'
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(community),
UdpTransportTarget((router_ip, 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)),
)
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
device_name = varBind[1].prettyPrint()
print(device_name)
,错误是:
python3 snmp.py
Traceback (most recent call last):
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 505, in resolveWithMib
instIds = rowNode.getInstIdFromIndices(*self.__args[2:])
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/mibs/SNMPv2-SMI.py", line 1281, in getInstIdFromIndices
syntax = mibObj.syntax.clone(indices[idx])
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/proto/rfc1902.py", line 232, in clone
return univ.OctetString.clone(self, *args, **kwargs).setFixedLength(self.getFixedLength())
File "/home/lamzaks/.local/lib/python3.10/site-packages/pyasn1/type/base.py", line 376, in clone
return self.__class__(value, **initializers)
File "/home/lamzaks/.local/lib/python3.10/site-packages/pyasn1/type/univ.py", line 837, in __init__
base.SimpleAsn1Type.__init__(self, value, **kwargs)
File "/home/lamzaks/.local/lib/python3.10/site-packages/pyasn1/type/base.py", line 267, in __init__
value = self.prettyIn(value)
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/proto/rfc1902.py", line 330, in prettyIn
raise error.ProtocolError('Bad IP address syntax')
pysnmp.proto.error.ProtocolError: Bad IP address syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/lamzaks/snmp.py", line 9, in <module>
errorIndication, errorStatus, errorIndex, varBinds = next(
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/hlapi/asyncore/sync/cmdgen.py", line 108, in getCmd
cmdgen.getCmd(snmpEngine, authData, transportTarget,
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/hlapi/asyncore/cmdgen.py", line 130, in getCmd
vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/hlapi/varbinds.py", line 39, in makeVarBinds
__varBinds.append(varBind.resolveWithMib(mibViewController, ignoreErrors=False))
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 853, in resolveWithMib
self.__args[0].resolveWithMib(mibViewController)
File "/home/lamzaks/.local/lib/python3.10/site-packages/pysnmp/smi/rfc1902.py", line 509, in resolveWithMib
raise SmiError('Instance index %r to OID conversion failure at object %r: %s' % (
pysnmp.smi.error.SmiError: Instance index (0,) to OID conversion failure at object 'ipAdEntAddr': Bad IP address syntaxcaused by <class 'pysnmp.proto.error.ProtocolError'>: Bad IP address syntax
此错误的原因是什么,如何解决?
我正在尝试从SNMP设备获取信息…如果我使用SNMPwalk -v2c -c public,它可以工作
根本原因
背后的原因可能是您正在测试的SNMP代理中的错误,
https://github.com/lextudio/pysnmp/blob/v5.0.20/pysnmp/proto/rfc1902.py L322
您可以从其源代码中看到,当它试图解析来自SNMP代理的传入数据时,它只是期望所有IP数据仅为IPv4。但是,这个代理似乎返回了意想不到的字节。
进一步挖掘的提示
错误消息中没有显示意外的字节,所以如果你想更深入地挖掘,你需要用Wireshark这样的工具捕获网络数据包。然后,您可以轻松地在Wireshark GUI中分析字节。
或者
snmpwalk
有一个转储原始字节的开关。但是您需要成为SNMP专家才能以原始格式读取这些字节。
那里有相当多的非标准SNMP代理,所以这样的问题是常见的。如果这个代理试图返回IPv6地址,它应该使用
Ipv6Address
文本约定,而不是IP
数据类型,https://datatracker.ietf.org/doc/html/rfc2465
为什么snmpwalk
可以工作?
其他流行的SNMP库在这种情况下可能更宽容。例如,snmpwalk
命令行实用程序基于用c编写的NET-SNMP库。