summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorelie <elie>2014-11-04 20:21:48 +0000
committerelie <elie>2014-11-04 20:21:48 +0000
commit963a6d644e4a00ed82adadfc09b535432061f0cd (patch)
tree31551e113a0b1403515f55947c12fbac57a5f7ff /examples
parent08c9b6df27aa45b442e0a799049e33c31ff486da (diff)
downloadpysnmp-git-963a6d644e4a00ed82adadfc09b535432061f0cd.tar.gz
initial support for asyncio network transport added
Diffstat (limited to 'examples')
-rw-r--r--examples/v3arch/asyncio/agent/cmdrsp/multiple-usm-users.py77
-rw-r--r--examples/v3arch/asyncio/manager/ntfrcv/multiple-interfaces.py71
2 files changed, 148 insertions, 0 deletions
diff --git a/examples/v3arch/asyncio/agent/cmdrsp/multiple-usm-users.py b/examples/v3arch/asyncio/agent/cmdrsp/multiple-usm-users.py
new file mode 100644
index 00000000..b41fc498
--- /dev/null
+++ b/examples/v3arch/asyncio/agent/cmdrsp/multiple-usm-users.py
@@ -0,0 +1,77 @@
+#
+# Command Responder
+#
+# Listen and respond to SNMP GET/SET/GETNEXT/GETBULK queries with
+# the following options:
+#
+# * SNMPv3
+# * with USM user 'usr-md5-des', auth: MD5, priv DES or
+# with USM user 'usr-sha-none', auth: SHA, no privacy
+# with USM user 'usr-sha-aes128', auth: SHA, priv AES
+# * allow access to SNMPv2-MIB objects (1.3.6.1.2.1)
+# * over IPv4/UDP, listening at 127.0.0.1:161
+# * using asyncio network transport (available since Python 3.4)
+#
+# Either of the following Net-SNMP's commands will walk this Agent:
+#
+# $ snmpwalk -v3 -u usr-md5-des -l authPriv -A authkey1 -X privkey1 localhost .1.3.6
+# $ snmpwalk -v3 -u usr-sha-none -l authNoPriv -a SHA -A authkey1 localhost .1.3.6
+# $ snmpwalk -v3 -u usr-sha-aes128 -l authPriv -a SHA -A authkey1 -x AES -X privkey1 localhost .1.3.6
+#
+from pysnmp.entity import engine, config
+from pysnmp.entity.rfc3413 import cmdrsp, context
+from pysnmp.carrier.asyncio.dgram import udp
+import asyncio
+
+# Get the event loop for this thread
+loop = asyncio.get_event_loop()
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+# Transport setup
+
+# UDP over IPv4
+config.addTransport(
+ snmpEngine,
+ udp.domainName,
+ udp.UdpTransport().openServerMode(('127.0.0.1', 161))
+)
+
+# SNMPv3/USM setup
+
+# user: usr-md5-des, auth: MD5, priv DES
+config.addV3User(
+ snmpEngine, 'usr-md5-des',
+ config.usmHMACMD5AuthProtocol, 'authkey1',
+ config.usmDESPrivProtocol, 'privkey1'
+)
+# user: usr-sha-none, auth: SHA, priv NONE
+config.addV3User(
+ snmpEngine, 'usr-sha-none',
+ config.usmHMACSHAAuthProtocol, 'authkey1'
+)
+# user: usr-sha-none, auth: SHA, priv AES
+config.addV3User(
+ snmpEngine, 'usr-sha-aes128',
+ config.usmHMACSHAAuthProtocol, 'authkey1',
+ config.usmAesCfb128Protocol, 'privkey1'
+)
+
+# Allow full MIB access for each user at VACM
+config.addVacmUser(snmpEngine, 3, 'usr-md5-des', 'authPriv', (1,3,6,1,2,1), (1,3,6,1,2,1))
+config.addVacmUser(snmpEngine, 3, 'usr-sha-none', 'authNoPriv', (1,3,6,1,2,1), (1,3,6,1,2,1))
+config.addVacmUser(snmpEngine, 3, 'usr-sha-aes128', 'authPriv', (1,3,6,1,2,1), (1,3,6,1,2,1))
+
+# Get default SNMP context this SNMP engine serves
+snmpContext = context.SnmpContext(snmpEngine)
+
+# Register SNMP Applications at the SNMP engine for particular SNMP context
+cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
+cmdrsp.SetCommandResponder(snmpEngine, snmpContext)
+cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
+cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)
+
+# Run asyncio main loop
+loop.run_forever()
diff --git a/examples/v3arch/asyncio/manager/ntfrcv/multiple-interfaces.py b/examples/v3arch/asyncio/manager/ntfrcv/multiple-interfaces.py
new file mode 100644
index 00000000..e84edc50
--- /dev/null
+++ b/examples/v3arch/asyncio/manager/ntfrcv/multiple-interfaces.py
@@ -0,0 +1,71 @@
+#
+# Notification Receiver
+#
+# Receive SNMP TRAP/INFORM messages with the following options:
+#
+# * SNMPv1/SNMPv2c
+# * with SNMP community "public"
+# * over IPv4/UDP, listening at 127.0.0.1:162
+# over IPv4/UDP, listening at 127.0.0.1:2162
+# * using Asyncio framework for network transport
+# * print received data on stdout
+#
+# Either of the following Net-SNMP's commands will send notifications to this
+# receiver:
+#
+# $ snmptrap -v2c -c public 127.0.0.1:162 123 1.3.6.1.6.3.1.1.5.1 1.3.6.1.2.1.1.5.0 s test
+# $ snmpinform -v2c -c public 127.0.0.1:2162 123 1.3.6.1.6.3.1.1.5.1
+#
+from pysnmp.entity import engine, config
+from pysnmp.carrier.asyncio.dgram import udp
+from pysnmp.entity.rfc3413 import ntfrcv
+import asyncio
+
+# Get the event loop for this thread
+loop = asyncio.get_event_loop()
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+# Transport setup
+
+# UDP over IPv4, first listening interface/port
+config.addTransport(
+ snmpEngine,
+ udp.domainName + (1,),
+ udp.UdpTransport().openServerMode(('127.0.0.1', 162))
+)
+
+# UDP over IPv4, second listening interface/port
+config.addTransport(
+ snmpEngine,
+ udp.domainName + (2,),
+ udp.UdpTransport().openServerMode(('127.0.0.1', 2162))
+)
+
+# SNMPv1/2c setup
+
+# SecurityName <-> CommunityName mapping
+config.addV1System(snmpEngine, 'my-area', 'public')
+
+# Callback function for receiving notifications
+def cbFun(snmpEngine,
+ stateReference,
+ contextEngineId, contextName,
+ varBinds,
+ cbCtx):
+ transportDomain, transportAddress = snmpEngine.msgAndPduDsp.getTransportInfo(stateReference)
+ print('Notification from %s, SNMP Engine %s, Context %s' % (
+ transportAddress, contextEngineId.prettyPrint(),
+ contextName.prettyPrint()
+ )
+ )
+ for name, val in varBinds:
+ print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
+
+# Register SNMP Application at the SNMP engine
+ntfrcv.NotificationReceiver(snmpEngine, cbFun)
+
+# Run asyncio main loop
+loop.run_forever()