1

ツリー階層内の特定のOIDの次のアイテムのみを取得するための単純なsnmpGETNEXTクエリを作成しようとしています。

たとえば、私が欲しいのは次のとおりです。

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、または mib の終わりまでのすべての OID を取得することによって機能します。

あなたが望むことを行う1つの方法は、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 に答える