如何解决pysnmp:没有此类名称错误此 OID 当前不存在此类对象 输出测试
我正在尝试通过 pysnmp 获取 IF-MIB 值,并且在此 OID 中当前不存在此类对象。但是,我使用的代码与示例代码非常相似,所以我不确定出了什么问题。任何人都可以看到这个问题吗? 1.3.6.1.2.1.2.2.1.6应该是得到IF地址(http://oid-info.com/get/1.3.6.1.2.1.2.2.1.6)
from pysnmp.hlapi import *
from pysnmp.smi import builder,view
# snmp.live.gambitcommunications.com
# demo.snmplabs.com
# snmpsim.try.thola.io
# localhost
MIB_VIEW_CONTROLLER = view.MibViewController(builder.MibBuilder())
g = getCmd(SnmpEngine(),CommunityData('public'),UdpTransportTarget(('127.0.0.1',161)),ContextData(),ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.6')),lookupNames=True,lookupValues=True)
errorIndication,errorStatus,errorIndex,varBinds = next(g)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
print("hello")
for varBind in varBinds:
print(type(varBind))
print(' = '.join([x.prettyPrint() for x in varBind]))
解决方法
当 getCmd
指向 OID
定义时,您请求 object-type
(获取查询)是错误的。在 getCmd
或 nextCmd
(步行操作)之间选择。
在这种情况下,如果你不知道这个对象的表的leaf
,你需要做一个walk操作,像这样:
from pysnmp.hlapi import *
iterator = nextCmd(
SnmpEngine(),CommunityData('public'),UdpTransportTarget(('127.0.0.1',161)),ContextData(),ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.6')),lookupMib=True,lookupValues=True
)
for response in iterator:
errorIndication,errorStatus,errorIndex,varBinds = response
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:
print(' = '.join([x.prettyPrint() for x in varBind]))
输出
python py37/main.py
SNMPv2-SMI::mib-2.2.2.1.6.1 =
SNMPv2-SMI::mib-2.2.2.1.6.2 = 0x1c697a6253c4
SNMPv2-SMI::mib-2.2.2.1.6.3 = 0x98af6511203d
SNMPv2-SMI::mib-2.2.2.1.6.4 = 0xa0cec8334d92
SNMPv2-SMI::mib-2.2.2.1.6.5 = 0x024247d306f5
SNMPv2-SMI::mib-2.2.2.1.6.6 = 0x02429213f7c0
SNMPv2-SMI::mib-2.2.2.1.6.7 = 0x02427d420a25
SNMPv2-SMI::mib-2.2.2.1.6.8 = 0x0242f5cb5e09
SNMPv2-SMI::mib-2.2.2.1.6.9 = 0x0242c6fba2ad
SNMPv2-SMI::mib-2.2.2.1.6.10 = 0x0242051d57ea
[...]
如果您知道要查询的叶子,请将其...放入您的代码中(使用 getCmd
),例如:
ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.6.1')),
测试
为了测试和学习 porpuses,您可以使用 bash 命令帮助自己:snmpwalk -v 1 -c public 127.0.0.1
要么
snmpget -v 1 -c public 127.0.0.1 1.3.6.1.2.1.2.2.1.6.1
它会给你音轨
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。