我正在尝试进行一个简单的 snmp GETNEXT 查询,以仅检索树层次结构中给定 OID 的下一项。
例如,我想要的是:
当我使用 OID 1.3.6.1.2.1.1 (iso.org.dod.internet.mgmt.mib-2.system)发出GETNEXT请求时
我希望得到一个响应,包括 OID 1.3.6.1.2.1.1.1.0 (iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0) 及其相应的值。
事实是:
为了检索单个下一个值,PySNMP 在1.3.6.1.2.1.1下执行 SNMP 遍历并检索所有子项。
如何更改此行为并使其仅返回单个下一个值而不是执行 snmpwalk?
我使用以下代码,该代码取自 PySNMP 的文档。
# GETNEXT Command Generator
from pysnmp.entity.rfc3413.oneliner import cmdgen
errorIndication, errorStatus, errorIndex, \
varBindTable = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('test-agent', 'public'),
cmdgen.UdpTransportTarget(('localhost', 161)),
(1,3,6,1,2,1,1)
)
if errorIndication:
print errorIndication
else:
if errorStatus:
print '%s at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
print '%s = %s' % (name.prettyPrint(), val.prettyPrint())