2017-09-22 34 views
0

私はpythonを初めて使用しています。私はpysnmpの使い方を理解しようとしています。pysnmp - nextCmd - 次の要素を取得していません

私は次のことを試してみました:

import asyncio 
from pysnmp.hlapi.asyncio import * 
from pysnmp import debug 

@asyncio.coroutine 
def run(): 
    snmpEngine = SnmpEngine() 
    while True: 
     errorIndication, errorStatus, errorIndex, varBinds = yield from nextCmd(
      snmpEngine, 
      CommunityData('public', mpModel=1), 
      UdpTransportTarget(('giga-int-2', 161)), 
      ContextData(), 
      ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.1')), 
      lexicographicMode=False 
     ) 

     if errorIndication: 
      print(errorIndication) 
      break 
     elif errorStatus: 
      print('%s at %s' % (
       errorStatus.prettyPrint(), 
       errorIndex and varBinds[int(errorIndex) - 1][0] or '?') 
      ) 
     else: 
      for varBind in varBinds: 
       for v in varBind: 
        print(' = '.join([x.prettyPrint() for x in v])) 

    snmpEngine.transportDispatcher.closeDispatcher() 

asyncio.get_event_loop().run_until_complete(run()) 

私はいつも同じインターフェイスを取得結果。どうしましたか?なぜ次の要素を取得しないのですか?

SNMPv2-SMI::mib-2.31.1.1.1.1.1 = sc0 
SNMPv2-SMI::mib-2.31.1.1.1.1.1 = sc0 
SNMPv2-SMI::mib-2.31.1.1.1.1.1 = sc0 
+0

別のOIDを試してみてください。ここで

は、あなたのアイデアを与えるために私のテストされていないコードです。あなたのコードはうまくいくようですが、いつも同じデバイス名を照会しています –

+0

私が達成したいのは、snmpwalk -v2c -cpublic giga-int-2 1.3.6.1.2.1.31.1.1.1と同じ出力です。 1 IF-MIB :: ifName.1 = STRING:sc0 IF-MIB :: ifName.2 = STRING:sl0 ... – Celio

+0

この場合、OID 1.3.6.1.2.1.31.1.1.1.1をループする必要があります、1.3.6.1.2.1.31.1.1.1.2、1.3.6.1.2.1.31.1.1.1.3など、私は思う。 –

答えて

0

これは機能します。しかし明示的にnext()を避けたいのですが、asyncioには適していません。

from pysnmp.hlapi import * 
from pysnmp import debug 
g = nextCmd(
    SnmpEngine(), 
    CommunityData('public', mpModel=1), 
    UdpTransportTarget(('giga-int-2', 161)), 
    ContextData(), 
    ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.1')), 
    lexicographicMode=False 
) 

while True: 
    try: 
     errorIndication, errorStatus, errorIndex, varBinds = next(g); 
     if errorIndication: 
      print(errorIndication) 
     elif errorStatus: 
      print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and  varBinds[int(errorIndex) - 1][0] or '?')) 
     else: 
      for name, val in varBinds: 
       print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) 
    except StopIteration: 
     break 
0

このコードがうまくいければ幸いです。これは非同期であり、明示的な呼び出しを含んでいません。あなたループ内

import asyncio 
from pysnmp.hlapi.asyncio import * 


@asyncio.coroutine 
def getone(snmpEngine, target): 
    errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd(
     snmpEngine, 
     CommunityData('public', mpModel=1), 
     UdpTransportTarget(('giga-int-2', 161)), 
     ContextData(), 
     target 
    ) 

    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])) 


snmpEngine = SnmpEngine() 

loop = asyncio.get_event_loop() 
loop.run_until_complete(
    asyncio.wait([getone(snmpEngine, ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.' + str(n)))) for n in range(1,254)]) 
+0

1.3.6.1.2.1.31.1.1.1.1.Xの下のすべてのoidが読み込まれたときは動作しますが、停止しません。それは1.3.6.1.2.1.31.1.1.1.2.xなどで続きます – Celio

+0

私の最終的な試み)それは私のために働く –

0

あなたは単一 SNMP GETNEXTコマンドを送信し、応答を受信して​​います。常に同じOIDに対して同じ要求を送信しています。これは、同じ回答を常に受け​​取っている理由を説明しています。 ;-)

あなたのSNMPエージェントを "歩き回る"ために、各反復時に、前回の反復で応答したばかりのOIDの要求を、あなたが停止するか(エージェントでOIDがなくなるまで) )。

start_oid = ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.1')) 
end_oid = ObjectType(ObjectIdentity('1.3.6.1.2.1.31.1.1.1.10')) 
oid = start_oid 

while oid < end_oid: 
    errorIndication, errorStatus, errorIndex, varBinds = yield from nextCmd(
     snmpEngine, 
     CommunityData('public', mpModel=1), 
     UdpTransportTarget(('giga-int-2', 161)), 
     ContextData(), 
     oid 
    ) 
    # ...omitted error checking... 
    varBind = varBinds[0] 
    # here we replacing the `oid` that goes into the next request 
    oid, value = varBind 
関連する問題