2

I try to to collect table async with PySnmp, but example give back only first record.

Could anyone suggest what is wrong?

import asyncio
from pysnmp.hlapi.asyncio import *

@asyncio.coroutine
def run():
    snmp_engine=SnmpEngine()
    count=0
    while True:
        errorIndication, errorStatus, errorIndex, varBinds = yield from nextCmd(
            snmp_engine,
            CommunityData('public'),
            UdpTransportTarget(('demo.snmplabs.com', 161)),
            ContextData(),
            ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')),
        )

        for next_var in varBinds:
            print(next_var[0])

        count+=1
        #break
        if count > 10:
            break

asyncio.get_event_loop().run_until_complete(run())  
Krisz
  • 701
  • 7
  • 23

2 Answers2

1

I have experienced same issue recently. I think the asynchronous nextCmd is not working like the sync nextCmd. You have to update the oid in each iteration. This code may help:

import asyncio
from pysnmp.hlapi.asyncio import *


def inc_oid(oid):
    _oid = oid.split(".")
    lastint = int(_oid[-1])
    _oid[-1] = str(lastint + 1)
    return ".".join(_oid)

snmp_engine=SnmpEngine()

@asyncio.coroutine
def snmp_walk_async(oid):
    endoid = inc_oid(oid)
    while True:
        errorIndication, errorStatus, errorIndex, varBinds = yield from nextCmd(
            snmp_engine,
            CommunityData("public"),
            UdpTransportTarget(("demo.snmplabs.com", 161)),
            ContextData(),
            ObjectType(ObjectIdentity(oid)),
            lexicographicMode=False,
        )

        #Some Error Checkings

        varBind = varBinds[0][0]
        oid, value = varBind
        oid = str(oid)

        if oid >= endoid:
            break

        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))


asyncio.get_event_loop().run_until_complete(snmp_walk_async("1.3.6.1.2.1.1"))  
kara
  • 130
  • 1
  • 11
0

If you read about how to query SNMP tables from a standard SNMP book, you will see that it was designed to be a few sync calls (GET NEXT or GET BULK), so you cannot run them in parallel.

But if you change your code to generate GET BULK requests, it will be a lot faster than generating GET NEXT requests.

Lex Li
  • 60,503
  • 9
  • 116
  • 147