question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

asyncio bulkwalk stuck in loop

See original GitHub issue

I am having issues with a snmp bulkwalk stoping correctly in asyncio. To me, it seems like the part that is suppose to stop the loop is the value of the isEndOfMib var. However, it keeps scanning OIDs that are way beyond it’s range of the mib. I tried a couple other mibs, but it is the same issues.

Here is where I based my example off of.

http://snmplabs.com/pysnmp/examples/hlapi/asyncio/manager/cmdgen/walking-operations.html

import asyncio
from pysnmp.hlapi.asyncio import *


@asyncio.coroutine
def run(varBinds):
    snmpEngine = SnmpEngine()
    while True:
        (errorIndication,
         errorStatus,
         errorIndex,
         varBindTable) = yield from bulkCmd(
            snmpEngine,
            CommunityData('public', mpModel=0),
            UdpTransportTarget(('10.161.105.59', 161)),
            ContextData(),
            0, 50,
            *varBinds)

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

        varBinds = varBindTable[-1]
        if isEndOfMib(varBinds):
            break

    snmpEngine.transportDispatcher.closeDispatcher()


loop = asyncio.get_event_loop()
loop.run_until_complete(
    run([ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))])
)

SNMPv2-MIB::sysDescr.0 = Cisco IOS Software, IOSv Software (VIOS-ADVENTERPRISEK9-M), Version 15.6(2)T, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2016 by Cisco Systems, Inc.
Compiled Tue 22-Mar-16 16:19 by prod_rel_team
SNMPv2-MIB::sysObjectID.0 = SNMPv2-SMI::enterprises.9.1.1041
SNMPv2-MIB::sysUpTime.0 = 239948
SNMPv2-MIB::sysContact.0 = 
SNMPv2-MIB::sysName.0 = IOS.test.com
SNMPv2-MIB::sysLocation.0 = 
SNMPv2-MIB::sysServices.0 = 78
SNMPv2-MIB::sysORLastChange.0 = 0
SNMPv2-MIB::sysORID.1 = SNMPv2-SMI::enterprises.9.7.129
SNMPv2-MIB::sysORID.2 = SNMPv2-SMI::enterprises.9.7.115
SNMPv2-MIB::sysORID.3 = SNMPv2-SMI::enterprises.9.7.265
SNMPv2-MIB::sysORID.4 = SNMPv2-SMI::enterprises.9.7.112
SNMPv2-MIB::sysORID.5 = SNMPv2-SMI::enterprises.9.7.106
SNMPv2-MIB::sysORID.6 = SNMPv2-SMI::enterprises.9.7.47
SNMPv2-MIB::sysORID.7 = SNMPv2-SMI::enterprises.9.7.122
SNMPv2-MIB::sysORID.8 = SNMPv2-SMI::enterprises.9.7.37
SNMPv2-MIB::sysORID.9 = SNMPv2-SMI::enterprises.9.7.92
SNMPv2-MIB::sysORID.10 = SNMPv2-SMI::enterprises.9.7.53
SNMPv2-MIB::sysORID.11 = SNMPv2-SMI::enterprises.9.7.54
SNMPv2-MIB::sysORID.12 = SNMPv2-SMI::enterprises.9.7.52
SNMPv2-MIB::sysORID.13 = SNMPv2-SMI::enterprises.9.7.93
SNMPv2-MIB::sysORID.14 = SNMPv2-SMI::enterprises.9.7.186
SNMPv2-MIB::sysORID.15 = SNMPv2-SMI::enterprises.9.7.128
SNMPv2-MIB::sysORID.16 = SNMPv2-SMI::enterprises.9.7.425
SNMPv2-MIB::sysORID.17 = SNMPv2-SMI::enterprises.9.7.517
SNMPv2-MIB::sysORID.18 = SNMPv2-SMI::enterprises.9.7.516
SNMPv2-MIB::sysORID.19 = SNMPv2-SMI::enterprises.9.7.518
Will not exit loop.

Here is a example when I ran it with snmpbulk command line.

[john@johnxps mibs]$ snmpbulkwalk -v2c -c public -Oq 10.161.105.59  sysDescr
SNMPv2-MIB::sysDescr.0 Cisco IOS Software, IOSv Software (VIOS-ADVENTERPRISEK9-M), Version 15.6(2)T, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2016 by Cisco Systems, Inc.
Compiled Tue 22-Mar-16 16:19 by prod_rel_team

Any help is appreciated.

Thank you,

John

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
etingofcommented, Jan 29, 2019

BTW, mpModel=0 means SNMP v1 which does not implement GETBULK so GETNEXT is being used instead.

1reaction
etingofcommented, Jan 30, 2019

Oh, I was wrong about lexicographicMode - this option is only available in synchronous API. Sorry about that!

So if you want similar behavior with asyncio I am thinking you should do something like this:

@asyncio.coroutine
def run(varBinds):
    snmpEngine = SnmpEngine()
    initialVarBinds = varBinds
    while True:
        (errorIndication,
         errorStatus,
         errorIndex,
         varBindTable) = yield from bulkCmd(
            snmpEngine,
            CommunityData('public', mpModel=0),
            UdpTransportTarget(('demo.snmplabs.com', 161)),
            ContextData(),
            0, 50,
            *varBinds)

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

        varBinds = varBindTable[-1]
        if isEndOfMib(varBinds):
            break

        for varBind in varBinds:
            if initialVarBinds[0][idx].isPrefixOf(varBind[0]):
                break

        else:
            break
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do my async functions get stuck in infinite loop?
I need the loop to be continuously fetching data, so I'm not awaiting any other function at any point. If I had await...
Read more >
asyncio basic event loop stuck with no tasks scheduled or ready
An exception is raised and propagates up the call stack without being consumed until the asyncio/events.py:_run function. The exception ...
Read more >
Common Mistakes Using Python3 asyncio
This "coroutine object" will be executed when current EventLoop gets a chance: awaited/yield from is called or all previous tasks are finished. To...
Read more >
How to Use Asyncio as_completed() in Python
This breaks the loop and is caught, reporting a message. This example highlights how we can impose a limit on how long the...
Read more >
How to ensure asyncio task exceptions always get logged
We often find it useful to schedule tasks (usually coroutines) to run concurrently in the event loop. Asyncio provides the create_task ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found