1

我正在尝试进行一个简单的 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())
4

2 回答 2

3

@Cankut,pysnmp 的“oneliner”GETNEXT API 通过检索给定前缀下的所有 OID 或所有 OID 直到 mib 结束来工作。

做你想做的事情的一种方法是用你自己的替换 pysnmp 的股票响应处理功能(这也需要使用更低级别的异步 API):

from pysnmp.entity.rfc3413.oneliner import cmdgen

def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex,
          varBindTable, cbCtx):
    if errorIndication:
        print(errorIndication)
        return 1
    if errorStatus:
        print(errorStatus.prettyPrint())
        return 1
    for varBindRow in varBindTable:
        for oid, val in varBindRow:
            print('%s = %s' % (oid.prettyPrint(),
                               val and val.prettyPrint() or '?'))

cmdGen  = cmdgen.AsynCommandGenerator()

cmdGen.nextCmd(
    cmdgen.CommunityData('test-agent', 'public'),
    cmdgen.UdpTransportTarget(('localhost', 161)),
    ((1,3,6,1,2,1,1),),
    (cbFun, None)
)

cmdGen.snmpEngine.transportDispatcher.runDispatcher()
于 2011-12-27T12:54:29.370 回答
-2
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdgen.CommandGenerator().nextCmd(
    cmdgen.CommunityData('test-agent', 'public'),
    cmdgen.UdpTransportTarget(('localhost', 161)),
    (1,3,6,1,2,1,1),maxRows=1
    )
于 2012-11-29T09:03:30.103 回答