简体   繁体   English

pysnmp:没有这样的名称错误(没有这样的 Object 当前存在于此 OID)

[英]pysnmp: No Such Name Error (No Such Object currently exists at this OID)

I am trying to get IF-MIB values via pysnmp, and am getting a No Such Object currently exists at this OID.我正在尝试通过 pysnmp 获取 IF-MIB 值,并且在此 OID 处当前存在 No such Object。 I'm using code very similar to the example code, however, so I'm not sure what's going wrong.但是,我使用的代码与示例代码非常相似,所以我不确定出了什么问题。 Can anyone see the issue?任何人都可以看到这个问题吗? 1.3.6.1.2.1.2.2.1.6 is supposed to get the IF address ( http://oid-info.com/get/1.3.6.1.2.1.2.2.1.6 ) 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]))

You are wrong asking for a getCmd ( Get query ) when the OID points to an object-type definition.OID指向object-type定义时,您要求getCmd ( Get query ) 是错误的。 Choose between getCmd or nextCmd ( walk operation).getCmdnextCmd (步行操作)之间进行选择。

In this case, if you don't know the leaf of the table of this object, you need to do a walk operation, like that:在这种情况下,如果你不知道这个 object 的表的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]))

output output

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
[...]

If you know the leaf to query, put it...in your code ( with getCmd ) like:如果您知道要查询的叶子,请将其放在您的代码中(使用getCmd ),例如:

    ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.6.1')),

testing测试

For testing and learning porpuses you can help yourself with bash command: snmpwalk -v 1 -c public 127.0.0.1 or snmpget -v 1 -c public 127.0.0.1 1.3.6.1.2.1.2.2.1.6.1对于测试和学习 porpuses,您可以使用 bash 命令帮助自己: snmpwalk -v 1 -c public 127.0.0.1snmpget -v 1 -c public 127.0.0.1 1.3.6.1.2.1.2.2.1.6.1

It will give you tracks它会给你曲目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM