etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
584 stars 202 forks source link

asyncio bulkwalk python 3.8 #343

Open lutfisan opened 4 years ago

lutfisan commented 4 years ago
import asyncio
from pysnmp.hlapi.asyncio import *

@asyncio.coroutine
def run(ip, varBinds):
    snmpEngine = SnmpEngine()
    initialVarBinds = varBinds
    while True:
        (errorIndication,
         errorStatus,
         errorIndex,
         varBindTable) = yield from nextCmd(
            snmpEngine,
            CommunityData('xlcom'),
            UdpTransportTarget((ip, 161)),
            ContextData(),
            *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

@asyncio.coroutine
def getall(ips):
    for ip in ips:
        yield from run(ip, [ObjectType(ObjectIdentity('1.3.6.1.2.1.4.20.1.3'))])

loop = asyncio.get_event_loop()
loop.run_until_complete(getall(['10.219.17.121',
                                '10.219.17.122',
                                '10.219.17.123']))

when I run this code, I get this warning

<ipython-input-26-bdc3d69277cf>:5: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
  def run(ip, varBinds):
<ipython-input-26-bdc3d69277cf>:46: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
  def getall(ips):

after I change to async def I get this error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-49d9606aa322> in <module>
     50 
     51 loop = asyncio.get_event_loop()
---> 52 loop.run_until_complete(getall(['10.219.17.121',
     53                                 '10.219.17.122',
     54                                 '10.219.17.123']))

~/miniconda3/envs/network/lib/python3.8/asyncio/base_events.py in run_until_complete(self, future)
    585 
    586         new_task = not futures.isfuture(future)
--> 587         future = tasks.ensure_future(future, loop=self)
    588         if new_task:
    589             # An exception is raised if the future didn't complete, so there

~/miniconda3/envs/network/lib/python3.8/asyncio/tasks.py in ensure_future(coro_or_future, loop)
    671         return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
    672     else:
--> 673         raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
    674                         'required')
    675 

TypeError: An asyncio.Future, a coroutine or an awaitable is required
lextm commented 1 year ago

You need more modification than that to use asyncio properly. This has been done in pysnmp-lextudio 5.0.22 and above.