summaryrefslogtreecommitdiff
path: root/examples/v3arch/twisted/manager/ntfrcv/v2c-multiple-interfaces.py
diff options
context:
space:
mode:
authorelie <elie>2013-03-16 17:05:46 +0000
committerelie <elie>2013-03-16 17:05:46 +0000
commitc4ada8eada8b4ea59a4bbb7d9c6123199d51917a (patch)
tree536f717c2f9a628c06684bb713b556705ff4671e /examples/v3arch/twisted/manager/ntfrcv/v2c-multiple-interfaces.py
parent19698bc00888b4cc59e0cec603a752c2e95fe849 (diff)
downloadpysnmp-c4ada8eada8b4ea59a4bbb7d9c6123199d51917a.tar.gz
a dozen of lightweight Twisted-based example scripts replaced more
complex example implementations used previously.
Diffstat (limited to 'examples/v3arch/twisted/manager/ntfrcv/v2c-multiple-interfaces.py')
-rw-r--r--examples/v3arch/twisted/manager/ntfrcv/v2c-multiple-interfaces.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/v3arch/twisted/manager/ntfrcv/v2c-multiple-interfaces.py b/examples/v3arch/twisted/manager/ntfrcv/v2c-multiple-interfaces.py
new file mode 100644
index 0000000..66f542a
--- /dev/null
+++ b/examples/v3arch/twisted/manager/ntfrcv/v2c-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
+# * 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 twisted.internet import reactor
+from pysnmp.entity import engine, config
+from pysnmp.carrier.twisted import dispatch
+from pysnmp.carrier.twisted.dgram import udp
+from pysnmp.entity.rfc3413 import ntfrcv
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+# Transport setup
+
+# Instantiate and register Twisted dispatcher at SNMP engine
+snmpEngine.registerTransportDispatcher(dispatch.TwistedDispatcher())
+
+# UDP over IPv4, first listening interface/port
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (1,),
+ udp.UdpTwistedTransport().openServerMode(('127.0.0.1', 162))
+)
+
+# UDP over IPv4, second listening interface/port
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (2,),
+ udp.UdpTwistedTransport().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 Twisted main loop
+reactor.run()