【问题标题】:pysnmp: No Such Name Error (No Such Object currently exists at this OID)pysnmp:没有此类名称错误(此 OID 处当前不存在此类对象)
【发布时间】:2021-04-02 16:02:52
【问题描述】:

我正在尝试通过 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]))

【问题讨论】:

    标签: python networking snmp pysnmp


    【解决方案1】:

    OID 指向object-type 定义时,您要求getCmd(获取查询)是错误的。在getCmdnextCmd 之间选择(步行操作)。

    在这种情况下,如果你不知道这个对象的表的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

    它会给你轨迹

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2019-12-29
    相关资源
    最近更新 更多