[英]How to get the value of OID in Python using PySnmp
Using snmpwalk
I can get this from my device: 使用
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
I tried this program in Python to get the value field from above OID: 我在Python中尝试了该程序,以从OID上方获取value字段:
#!/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')
Output i get: 输出我得到:
When i run the program, it shows a long list of OID's (don't know why even I am giving the leaf level OID as input in program) with values. 当我运行程序时,它会显示一长串带有值的OID(甚至不知道为什么我还给叶级OID作为程序中的输入)。 STRANGE.
奇怪。
What is tried 尝试了什么
lexicographicMode=True
in the nextCmd
but it doesn't show anything than. lexicographicMode=True
在nextCmd
但没有显示任何内容。
What i wish 我希望
I want to give a list of OID in my program and wants their values(value is a key you can see in first line), that's it. 我想在程序中提供OID列表,并希望它们的值(值是您可以在第一行中看到的键),仅此而已。
Request 请求
Please help me in python program to do so using pysnmp. 请使用pysnmp在python程序中帮助我。
If you want OIDs, use mibLookup=False parameter. 如果需要OID,请使用mibLookup = False参数。 If you want just the branch of the MIB, use
lexicographicMode=False
, but make sure to specify non-leaf OID because in that case you will get nothing in return. 如果只需要MIB的分支,请使用
lexicographicMode=False
,但是请确保指定非叶子OID,因为在这种情况下,您将一无所获。
Here's your script with the suggested changes: 这是您的脚本,其中包含建议的更改:
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')
You should be able to cut&paste it, it's running against public SNMP simulator at demo.snmplabs.com . 您应该能够剪切并粘贴它,它可以在demo.snmplabs.com上针对公共SNMP仿真器运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.