summaryrefslogtreecommitdiff
path: root/examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2019-02-22 06:43:56 +0100
committerGitHub <noreply@github.com>2019-02-22 06:43:56 +0100
commit74fcc27b038da72c6aa9e2c08b4dac63175b8b5f (patch)
tree0c16c866ba88bf594b2ddea1f54fffe010b66ea8 /examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py
parent0cf85fdffc601477afb4a9c5568653154fb4eb32 (diff)
downloadpysnmp-git-74fcc27b038da72c6aa9e2c08b4dac63175b8b5f.tar.gz
Introduce asyncio binding to hlapi.v1arch (#244)
The hlapi.v1arch asyncio API is intended to be very similar to hlapi.v3arch.asyncio from its signature viewpoint, however it should be faster at the expense of no SNMPv3 support.
Diffstat (limited to 'examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py')
-rw-r--r--examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py b/examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py
new file mode 100644
index 00000000..3ce3c232
--- /dev/null
+++ b/examples/hlapi/v1arch/asyncio/manager/cmdgen/multiple-sequential-queries.py
@@ -0,0 +1,68 @@
+"""
+Sequential queries
+++++++++++++++++++
+
+Send multiple SNMP GET requests one by one using the following options:
+
+* with SNMPv2c, community 'public'
+* over IPv4/UDP
+* to multiple Agents at demo.snmplabs.com
+* for instance of SNMPv2-MIB::sysDescr.0 MIB object
+* based on asyncio I/O framework
+
+Functionally similar to:
+
+| $ snmpget -v2c -c public demo.snmplabs.com:1161 SNMPv2-MIB::sysDescr.0
+| $ snmpget -v2c -c public demo.snmplabs.com:2161 SNMPv2-MIB::sysDescr.0
+| $ snmpget -v2c -c public demo.snmplabs.com:3161 SNMPv2-MIB::sysDescr.0
+
+"""#
+import asyncio
+from pysnmp.hlapi.v1arch.asyncio import *
+
+
+@asyncio.coroutine
+def getone(snmpDispatcher, hostname):
+
+ iterator = getCmd(
+ snmpDispatcher,
+ CommunityData('public'),
+ UdpTransportTarget(hostname),
+ ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
+ )
+
+ errorIndication, errorStatus, errorIndex, varBinds = yield from iterator
+
+ if errorIndication:
+ print(errorIndication)
+
+ elif errorStatus:
+ print('%s at %s' % (
+ errorStatus.prettyPrint(),
+ errorIndex and varBinds[int(errorIndex) - 1][0] or '?'
+ )
+ )
+ else:
+ for varBind in varBinds:
+ print(' = '.join([x.prettyPrint() for x in varBind]))
+
+
+@asyncio.coroutine
+def getall(snmpDispatcher, hostnames):
+ for hostname in hostnames:
+ yield from getone(snmpDispatcher, hostname)
+
+
+snmpDispatcher = SnmpDispatcher()
+
+loop = asyncio.get_event_loop()
+
+loop.run_until_complete(
+ getall(
+ snmpDispatcher, [
+ ('demo.snmplabs.com', 1161),
+ ('demo.snmplabs.com', 2161),
+ ('demo.snmplabs.com', 3161)
+ ]
+ )
+)