我提出异常,但我不明白为什么
snimpy.snmp.SNMPNoSuchObject: No such object was found
from snimpy import manager as snimpy
def snmp(hostname, oids, mibs):
logger.debug(hostname)
logger.debug(oids)
logger.debug(mibs)
for mib in mibs:
snimpy.load(mib)
session = snimpy.snmp.Session(hostname, "public", 1)
details = session.get(*oids)
return [{
'oid': '.' + '.'.join(repr(node) for node in oid[0]),
'value': oid[1]
} for oid in details]
oids = ['.1.3.6.1.2.1.25.3.2.1.3.1', '.1.3.6.1.2.1.43.10.2.1.4.1.1.1.3.6.1.2.1.1.4.0', '.1.3.6.1.2.1.1.1.0', '.1.3.6.1.2.1.1.5.0', '.1.3.6.1.2.1.1.3.0']
hostname = '192.168.2.250'
mibs = ['DISMAN-EVENT-MIB', 'HOST-RESOURCES-MIB', 'SNMPv2-MIB', 'SNMPv2-SMI']
snmp(hostname, oids, mibs)
>>> scanner.get_device_infos('192.168.2.250')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "~/project/daemon/api/scanner.py", line 62, in get_device_infos
infos = self.network_tools.snmp(hostname, oids, mibs)
File "~/project/daemon/api/network_tools.py", line 26, in snmp
logger.debug(type(oids))
File "~/project/env/lib/python3.5/site-packages/snimpy/snmp.py", line 286, in get
return self._op(self._cmdgen.getCmd, *oids)
File "~/project/env/lib/python3.5/site-packages/snimpy/snmp.py", line 278, in _op
return tuple([(oid, self._convert(val)) for oid, val in results])
File "~/project/env/lib/python3.5/site-packages/snimpy/snmp.py", line 278, in <listcomp>
return tuple([(oid, self._convert(val)) for oid, val in results])
File "~/project/env/lib/python3.5/site-packages/snimpy/snmp.py", line 249, in _convert
self._check_exception(value)
File "~/project/env/lib/python3.5/site-packages/snimpy/snmp.py", line 217, in _check_exception
raise SNMPNoSuchObject("No such object was found") # nopep8
snimpy.snmp.SNMPNoSuchObject: No such object was found
使用bash
和snmpget
命令到达设备可以正常工作:
declare -a oids=(
'.1.3.6.1.2.1.25.3.2.1.3.1' # HOST-RESOURCES-MIB::hrDeviceDescr.1
'.1.3.6.1.2.1.43.10.2.1.4.1.1' # SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 page count
'.1.3.6.1.2.1.1.4.0' # SNMPv2-MIB::sysContact.0
'.1.3.6.1.2.1.1.1.0' # SNMPv2-MIB::sysDescr.0
'.1.3.6.1.2.1.1.5.0' # SNMPv2-MIB::sysName.0
'.1.3.6.1.2.1.1.3.0' # DISMAN-EVENT-MIB::sysUpTimeInstance
)
for oid in ${oids[@]}; do
echo "$oid"
snmpget -v 1 -t .3 -r 2 -c public 192.168.2.250 -m +SNMPv2-MIB "$oid"
echo
done
输出:
.1.3.6.1.2.1.25.3.2.1.3.1
HOST-RESOURCES-MIB::hrDeviceDescr.1 = STRING: Brother HL-5250DN series
.1.3.6.1.2.1.43.10.2.1.4.1.1
SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 = Counter32: 22629
.1.3.6.1.2.1.1.4.0
SNMPv2-MIB::sysContact.0 = STRING:
.1.3.6.1.2.1.1.1.0
SNMPv2-MIB::sysDescr.0 = STRING: Brother NC-6400h, Firmware Ver.1.01 (05.08.31),MID 84UZ92
.1.3.6.1.2.1.1.5.0
SNMPv2-MIB::sysName.0 = STRING: BRN_7D3B43
.1.3.6.1.2.1.1.3.0
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (168019770) 19 days, 10:43:17.70
这里有什么问题?
答案 0 :(得分:0)
其中一个oid
似乎触发了错误(参见'.1.3.6.1.2.1.43.10.2.1.4.1.1' # SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 page count
),将其移动到上次修复错误。但这是一个可疑的解决方案。
oids = [
'.1.3.6.1.2.1.25.3.2.1.3.1', # HOST-RESOURCES-MIB::hrDeviceDescr.1
'.1.3.6.1.2.1.1.4.0', # SNMPv2-MIB::sysContact.0
'.1.3.6.1.2.1.1.1.0', # SNMPv2-MIB::sysDescr.0
'.1.3.6.1.2.1.1.5.0', # SNMPv2-MIB::sysName.0
'.1.3.6.1.2.1.1.3.0', # DISMAN-EVENT-MIB::sysUpTimeInstance
# ugly duckling
'.1.3.6.1.2.1.43.10.2.1.4.1.1' # SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 page count
]