Python PySNMP:无法获取OID
问题描述:
我似乎无法使用pysnmp获取SNMP。
与python2和3相同的结果。
设备使用SNMP v2。Python PySNMP:无法获取OID
SNMPv2-SMI::enterprises.5597.30.0.2.2 = No Such Object currently exists
at this OID
SNMPv2-SMI::enterprises.5597.30.0.2.4 = No Such Object currently exists
at this OID
虽然snmpwalk的正常工作:
snmpwalk -v1 -cpublic 10.0.1.8 1.3.6.1.4.1.5597.30.0.2.2
iso.3.6.1.4.1.5597.30.0.2.2.0 = INTEGER: 1
这是我的代码:
from pysnmp.entity.rfc3413.oneliner import cmdgen
import time
SNMP_HOST = '10.0.1.8'
SNMP_PORT = 161
SNMP_COMMUNITY = 'public'
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget((SNMP_HOST, SNMP_PORT)),
'1.3.6.1.4.1.5597.30.0.2.2',
'1.3.6.1.4.1.5597.30.0.2.4'
)
# Check for errors and print out results
if errorIndication:
print(errorIndication)
else:
if errorStatus:
print('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1] or '?'
)
)
else:
for name, val in varBinds:
print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
答
你正在做GET iso.3.6.1.4.1.5597.30.0.2.2
,而snmpwalk的报告说,仅OID iso.3.6.1.4.1.5597.30.0.2.2.0
存在。
试试这个代码(摘自this example)。它使用更新,更简洁的pysnmp API,但是一旦修复了查询的OID,您的代码也应该可以正常工作。
from pysnmp.hlapi import *
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public'),
UdpTransportTarget(('10.0.1.8', 161)),
ContextData(),
ObjectType(ObjectIdentity('1.3.6.1.4.1.5597.30.0.2.2.0')))
)
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]))
谢谢,现在我似乎回到了价值。有一个)在ObjectType(...)后缺少 – HyperDevil