如何使用PySnmp获取Python中OID的值

2024-10-01 13:41:25 发布

您现在位置:Python中文网/ 问答频道 /正文

使用snmpwalk我可以从我的设备中获取:

OID=.1.3.6.1.4.1.5296.1.9.1.1.1.7.115.101.99.99.97.57.27.1.41
Type=OctetString
Value=secca99

我在Python中尝试了这个程序,从上面的OID中获取值字段:

^{pr2}$

输出我得到:

当我运行这个程序时,它会显示一长串的OID(甚至不知道为什么我会在程序中给叶级OID作为输入)。奇怪。在

尝试了什么

lexicographicMode=True中的lexicographicMode=True,但它没有显示任何内容。在

我的愿望

我想在我的程序中给出一个OID列表,并想要它们的值(value是一个可以在第一行看到的键),就这样。在

请求

请在python程序中帮助我使用pysnmp。在


Tags: 程序true内容列表valuetypeoid我会
1条回答
网友
1楼 · 发布于 2024-10-01 13:41:25

如果需要OID,请使用mibLookup=False参数。如果只需要MIB的分支,请使用lexicographicMode=False,但一定要指定非叶OID,因为在这种情况下,您将得不到任何回报。在

以下是您的脚本和建议的更改:

from pysnmp.hlapi import *
import sys

def walk(host, oid):

    for (errorIndication,
         errorStatus,
         errorIndex,
         varBinds) in nextCmd(SnmpEngine(),
                              CommunityData('public'),
                              UdpTransportTarget((host, 161)),
                              ContextData(),
                              ObjectType(ObjectIdentity(oid)),
                              lookupMib=False,
                              lexicographicMode=False):

        if errorIndication:
            print(errorIndication, file=sys.stderr)
            break

        elif errorStatus:
            print('%s at %s' % (errorStatus.prettyPrint(),
                                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'), file=sys.stderr)
            break

        else:
            for varBind in varBinds:
                 print('%s = %s' % varBind)

walk('demo.snmplabs.com', '1.3.6.1.2.1.1.9.1.2')

您应该能够剪切并粘贴它,它正在运行于demo.snmplabs.com上的公共SNMP模拟器。在

相关问题 更多 >