From 0569329e22b6d41da9fa5a59eb8d430959c5a024 Mon Sep 17 00:00:00 2001 From: Lex Li Date: Tue, 12 Sep 2023 18:28:17 -0400 Subject: [PATCH] Updated examples. --- .../asyncio/agent/ntforg/default-v1-trap.py | 4 +-- .../ntforg/multiple-notifications-at-once.py | 3 +-- .../hlapi/asyncio/agent/ntforg/v3-inform.py | 4 +-- .../cmdgen/custom-asn1-mib-search-path.py | 3 +-- .../asyncio/manager/cmdgen/getbulk-to-eom.py | 4 +-- .../cmdgen/multiple-sequential-queries.py | 3 +-- .../hlapi/asyncio/manager/cmdgen/send-trap.py | 26 +++++++++++++++++++ .../asyncio/manager/cmdgen/usm-sha-aes128.py | 3 +-- pysnmp/hlapi/asyncio/slim.py | 16 +++--------- 9 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 examples/hlapi/asyncio/manager/cmdgen/send-trap.py diff --git a/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py b/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py index 6f1326b0e..40ee8e990 100644 --- a/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py +++ b/examples/hlapi/asyncio/agent/ntforg/default-v1-trap.py @@ -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)), @@ -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) diff --git a/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py b/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py index 3e863dcf8..4b6cc1022 100644 --- a/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py +++ b/examples/hlapi/asyncio/agent/ntforg/multiple-notifications-at-once.py @@ -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), @@ -39,7 +39,6 @@ async def sendone(snmpEngine, hostname, notifyType): ), ) - (errorIndication, errorStatus, errorIndex, varBinds) = await trap_result if errorIndication: print(errorIndication) elif errorStatus: diff --git a/examples/hlapi/asyncio/agent/ntforg/v3-inform.py b/examples/hlapi/asyncio/agent/ntforg/v3-inform.py index b6b4a1c8a..1d7efd3c1 100644 --- a/examples/hlapi/asyncio/agent/ntforg/v3-inform.py +++ b/examples/hlapi/asyncio/agent/ntforg/v3-inform.py @@ -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)), @@ -33,8 +33,6 @@ async def run(): ) ) - errorIndication, errorStatus, errorIndex, varBinds = await iterator - if errorIndication: print(errorIndication) diff --git a/examples/hlapi/asyncio/manager/cmdgen/custom-asn1-mib-search-path.py b/examples/hlapi/asyncio/manager/cmdgen/custom-asn1-mib-search-path.py index 840befcc3..40b951f2c 100644 --- a/examples/hlapi/asyncio/manager/cmdgen/custom-asn1-mib-search-path.py +++ b/examples/hlapi/asyncio/manager/cmdgen/custom-asn1-mib-search-path.py @@ -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)), @@ -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: diff --git a/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py b/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py index d27429941..f0d321271 100644 --- a/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py +++ b/examples/hlapi/asyncio/manager/cmdgen/getbulk-to-eom.py @@ -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)), @@ -33,7 +33,7 @@ async def run(varBinds): 50, *varBinds ) - (errorIndication, errorStatus, errorIndex, varBindTable) = await bulk_task + if errorIndication: print(errorIndication) break diff --git a/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py b/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py index 50c101e24..d2f836b01 100644 --- a/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py +++ b/examples/hlapi/asyncio/manager/cmdgen/multiple-sequential-queries.py @@ -22,7 +22,7 @@ async def getone(snmpEngine, hostname): - result_get = await getCmd( + errorIndication, errorStatus, errorIndex, varBinds = await getCmd( snmpEngine, CommunityData("public"), UdpTransportTarget(hostname), @@ -30,7 +30,6 @@ async def getone(snmpEngine, hostname): ObjectType(ObjectIdentity("SNMPv2-MIB", "sysDescr", 0)), ) - errorIndication, errorStatus, errorIndex, varBinds = await result_get if errorIndication: print(errorIndication) elif errorStatus: diff --git a/examples/hlapi/asyncio/manager/cmdgen/send-trap.py b/examples/hlapi/asyncio/manager/cmdgen/send-trap.py new file mode 100644 index 000000000..52c475476 --- /dev/null +++ b/examples/hlapi/asyncio/manager/cmdgen/send-trap.py @@ -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()) \ No newline at end of file diff --git a/examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py b/examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py index 835b7115f..00507d3c3 100644 --- a/examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py +++ b/examples/hlapi/asyncio/manager/cmdgen/usm-sha-aes128.py @@ -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, @@ -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: diff --git a/pysnmp/hlapi/asyncio/slim.py b/pysnmp/hlapi/asyncio/slim.py index 857e9d8e3..097373e24 100644 --- a/pysnmp/hlapi/asyncio/slim.py +++ b/pysnmp/hlapi/asyncio/slim.py @@ -105,7 +105,7 @@ 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)), @@ -113,8 +113,6 @@ async def get(self, communityName, address, port, *varBinds): *varBinds, ) - return await get_result - async def next(self, communityName, address, port, *varBinds): r"""Creates a generator to perform SNMP GETNEXT query. @@ -180,7 +178,7 @@ 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)), @@ -188,8 +186,6 @@ async def next(self, communityName, address, port, *varBinds): *varBinds, ) - return await next_result - async def bulk(self, communityName, address, port, nonRepeaters, maxRepetitions, *varBinds): r"""Creates a generator to perform SNMP GETBULK query. @@ -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)), @@ -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. @@ -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