Skip to content

Commit

Permalink
Merge pull request #24 from eLvErDe/fix-asyncio-hlapi
Browse files Browse the repository at this point in the history
Fix asyncio hlapi double awaitable returns (Fix #19)
  • Loading branch information
lextm authored Sep 12, 2023
2 parents 498d36b + 0569329 commit 5280c5c
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 42 deletions.
4 changes: 2 additions & 2 deletions examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

async def run():
snmpEngine = SnmpEngine()
trap_result = await sendNotification(
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
Expand All @@ -37,7 +37,7 @@ async def run():
("1.3.6.1.2.1.1.1.0", OctetString("my system")),
),
)
errorIndication, errorStatus, errorIndex, varBinds = await trap_result

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


async def sendone(snmpEngine, hostname, notifyType):
trap_result = await sendNotification(
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData("public", tag=hostname),
UdpTransportTarget((hostname, 162), tagList=hostname),
Expand All @@ -39,7 +39,6 @@ async def sendone(snmpEngine, hostname, notifyType):
),
)

(errorIndication, errorStatus, errorIndex, varBinds) = await trap_result
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
4 changes: 1 addition & 3 deletions examples/hlapi/asyncio/agent/ntforg/v3-inform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pysnmp.hlapi.asyncio.transport import UdpTransportTarget

async def run():
iterator = await sendNotification(
errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
SnmpEngine(),
UsmUserData('usr-md5-des', 'authkey1', 'privkey1'),
UdpTransportTarget(('demo.pysnmp.com', 162)),
Expand All @@ -33,8 +33,6 @@ async def run():
)
)

errorIndication, errorStatus, errorIndex, varBinds = await iterator

if errorIndication:
print(errorIndication)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

async def run():
snmpEngine = SnmpEngine()
get_result = await getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData('public'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
Expand All @@ -29,7 +29,6 @@ async def run():
'https://mibs.pysnmp.com/asn1/@mib@'))
)

errorIndication, errorStatus, errorIndex, varBinds = await get_result
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
4 changes: 2 additions & 2 deletions examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
async def run(varBinds):
snmpEngine = SnmpEngine()
while True:
bulk_task = await bulkCmd(
errorIndication, errorStatus, errorIndex, varBindTable = await bulkCmd(
snmpEngine,
UsmUserData('usr-none-none'),
UdpTransportTarget(('demo.pysnmp.com', 161)),
Expand All @@ -33,7 +33,7 @@ async def run(varBinds):
50,
*varBinds
)
(errorIndication, errorStatus, errorIndex, varBindTable) = await bulk_task

if errorIndication:
print(errorIndication)
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@


async def getone(snmpEngine, hostname):
result_get = await getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
CommunityData("public"),
UdpTransportTarget(hostname),
ContextData(),
ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)),
)

errorIndication, errorStatus, errorIndex, varBinds = await result_get
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
26 changes: 26 additions & 0 deletions examples/hlapi/asyncio/manager/cmdgen/send-trap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import asyncio
from pysnmp.hlapi.asyncio import *

async def run():
snmpEngine = SnmpEngine()
# Example of how you might update sysUpTime
mibBuilder = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder
sysUpTime, = mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
sysUpTime.syntax = TimeTicks(12345) # Set uptime to 12345

errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
snmpEngine,
CommunityData('public', mpModel=0),
UdpTransportTarget(('demo.pysnmp.com', 162)),
ContextData(),
"trap",
NotificationType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB', 'netSnmpExampleNotification')).addVarBinds(ObjectType(ObjectIdentity('NET-SNMP-EXAMPLES-MIB','netSnmpExampleHeartbeatRate'), 1))
)

if errorIndication:
print(errorIndication)

snmpEngine.transportDispatcher.closeDispatcher()


asyncio.run(run())
3 changes: 1 addition & 2 deletions examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

async def run():
snmpEngine = SnmpEngine()
get_result = await getCmd(
errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
snmpEngine,
UsmUserData('usr-sha-aes', 'authkey1', 'privkey1',
authProtocol=usmHMACSHAAuthProtocol,
Expand All @@ -38,7 +38,6 @@ async def run():
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

errorIndication, errorStatus, errorIndex, varBinds = await get_result
if errorIndication:
print(errorIndication)
elif errorStatus:
Expand Down
20 changes: 8 additions & 12 deletions pysnmp/hlapi/asyncio/cmdgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ async def getCmd(snmpEngine, authData, transportTarget, contextData,
>>> from pysnmp.hlapi.asyncio import *
>>>
>>> async def run():
... result_get = await getCmd(
... errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
... SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
... )
... errorIndication, errorStatus, errorIndex, varBinds = await result_get
... print(errorIndication, errorStatus, errorIndex, varBinds)
>>>
>>> asyncio.run(run())
Expand Down Expand Up @@ -156,7 +155,7 @@ def __cbFun(snmpEngine, sendRequestHandle,
vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
(options.get('lookupMib', True), future)
)
return future
return await future


async def setCmd(snmpEngine, authData, transportTarget, contextData,
Expand Down Expand Up @@ -218,14 +217,13 @@ async def setCmd(snmpEngine, authData, transportTarget, contextData,
>>> from pysnmp.hlapi.asyncio import *
>>>
>>> async def run():
... set_result = await setCmd(
... errorIndication, errorStatus, errorIndex, varBinds = await setCmd(
... SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0), 'Linux i386')
... )
... errorIndication, errorStatus, errorIndex, varBinds = await set_result
... print(errorIndication, errorStatus, errorIndex, varBinds)
>>>
>>> asyncio.run(run())
Expand Down Expand Up @@ -262,7 +260,7 @@ def __cbFun(snmpEngine, sendRequestHandle,
vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
(options.get('lookupMib', True), future)
)
return future
return await future


async def nextCmd(snmpEngine, authData, transportTarget, contextData,
Expand Down Expand Up @@ -328,14 +326,13 @@ async def nextCmd(snmpEngine, authData, transportTarget, contextData,
>>> from pysnmp.hlapi.asyncio import *
>>>
>>> async def run():
... next_result = await nextCmd(
... errorIndication, errorStatus, errorIndex, varBinds = await nextCmd(
... SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... ContextData(),
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))
... )
... errorIndication, errorStatus, errorIndex, varBinds = await next_result
... print(errorIndication, errorStatus, errorIndex, varBinds)
>>>
>>> asyncio.run(run())
Expand Down Expand Up @@ -374,7 +371,7 @@ def __cbFun(snmpEngine, sendRequestHandle,
vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
(options.get('lookupMib', True), future)
)
return future
return await future


async def bulkCmd(snmpEngine, authData, transportTarget, contextData,
Expand Down Expand Up @@ -468,15 +465,14 @@ async def bulkCmd(snmpEngine, authData, transportTarget, contextData,
>>> from pysnmp.hlapi.asyncio import *
>>>
>>> async def run():
... result_bulk = await bulkCmd(
... errorIndication, errorStatus, errorIndex, varBinds = await bulkCmd(
... SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 161)),
... ContextData(),
... 0, 2,
... ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))
... )
... errorIndication, errorStatus, errorIndex, varBinds = await result_bulk
... print(errorIndication, errorStatus, errorIndex, varBinds)
>>>
>>> asyncio.run(run())
Expand Down Expand Up @@ -515,4 +511,4 @@ def __cbFun(snmpEngine, sendRequestHandle,
vbProcessor.makeVarBinds(snmpEngine, varBinds), __cbFun,
(options.get('lookupMib', True), future)
)
return future
return await future
5 changes: 2 additions & 3 deletions pysnmp/hlapi/asyncio/ntforg.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ async def sendNotification(snmpEngine, authData, transportTarget, contextData,
>>> from pysnmp.hlapi.asyncio import *
>>>
>>> async def run():
... send_result = await sendNotification(
... errorIndication, errorStatus, errorIndex, varBinds = await sendNotification(
... SnmpEngine(),
... CommunityData('public'),
... UdpTransportTarget(('demo.pysnmp.com', 162)),
... ContextData(),
... 'trap',
... NotificationType(ObjectIdentity('IF-MIB', 'linkDown')))
... errorIndication, errorStatus, errorIndex, varBinds = await send_result
... print(errorIndication, errorStatus, errorIndex, varBinds)
...
>>> asyncio.run(run())
Expand Down Expand Up @@ -159,4 +158,4 @@ def __trapFun(future):
loop = asyncio.get_event_loop()
loop.call_soon(__trapFun, future)

return future
return await future
16 changes: 4 additions & 12 deletions pysnmp/hlapi/asyncio/slim.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,14 @@ async def get(self, communityName, address, port, *varBinds):
"""

get_result = await getCmd(
return await getCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=self.version - 1),
UdpTransportTarget((address, port)),
ContextData(),
*varBinds,
)

return await get_result

async def next(self, communityName, address, port, *varBinds):
r"""Creates a generator to perform SNMP GETNEXT query.
Expand Down Expand Up @@ -180,16 +178,14 @@ async def next(self, communityName, address, port, *varBinds):
"""

next_result = await nextCmd(
return await nextCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=self.version - 1),
UdpTransportTarget((address, port)),
ContextData(),
*varBinds,
)

return await next_result

async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions, *varBinds):
r"""Creates a generator to perform SNMP GETBULK query.
Expand Down Expand Up @@ -288,7 +284,7 @@ async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions,
version = self.version - 1
if version == 0:
raise PySnmpError('Cannot send V2 PDU on V1 session')
bulk_result = await bulkCmd(
return await bulkCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=version),
UdpTransportTarget((address, port)),
Expand All @@ -298,8 +294,6 @@ async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions,
*varBinds,
)

return await bulk_result

async def set(self, communityName, address, port, *varBinds):
r"""Creates a generator to perform SNMP SET query.
Expand Down Expand Up @@ -361,12 +355,10 @@ async def set(self, communityName, address, port, *varBinds):
"""

set_result = await setCmd(
return await setCmd(
self.snmpEngine,
CommunityData(communityName, mpModel=self.version - 1),
UdpTransportTarget((address, port)),
ContextData(),
*varBinds,
)

return await set_result

0 comments on commit 5280c5c

Please sign in to comment.