使用 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获取值字段:
#!/usr/bin/env python3
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))):
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(varBind)
walk('10.78.163.39',
'.1.3.6.1.4.1.5296.1.9.1.1.1.7.115.101.99.99.97.57.27.1.41')
输出我得到:
当我运行程序时,它显示一长串 OID(不知道为什么我在程序中将叶级 OID 作为输入)和值。奇怪。
尝试了什么
lexicographyMode=True
在 nextCmd
中,但它没有显示任何内容。
我的愿望
我想在我的程序中给出一个 OID 列表,并想要它们的值(值是你可以在第一行看到的键),就是这样。
请求
请帮助我在 python 程序中使用 pysnmp 来执行此操作。
最佳答案
如果您需要 OID,请使用 mibLookup=False范围。如果您只需要 MIB 的分支,请使用 lexicographyMode=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')
您应该能够剪切和粘贴它,它正在公共(public) SNMP 模拟器上运行 demo.snmplabs.com .
关于python - 如何在Python中使用PySnmp获取OID的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49137695/