summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorelie <elie>2014-01-25 16:01:53 +0000
committerelie <elie>2014-01-25 16:01:53 +0000
commit9f53657a29fa470fdc335ad56e497f9cee129f54 (patch)
tree6ce80041c2686c8e82b0cf62ed5544800be8116d /examples
parent219fedac62d83b74edcc5544a3a16d821f3f723d (diff)
downloadpysnmp-9f53657a29fa470fdc335ad56e497f9cee129f54.tar.gz
example script explaining incoming message's communityName re-writing added
Diffstat (limited to 'examples')
-rw-r--r--examples/v3arch/manager/ntfrcv/v2c-with-regexp-community-name.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/examples/v3arch/manager/ntfrcv/v2c-with-regexp-community-name.py b/examples/v3arch/manager/ntfrcv/v2c-with-regexp-community-name.py
new file mode 100644
index 0000000..abfb991
--- /dev/null
+++ b/examples/v3arch/manager/ntfrcv/v2c-with-regexp-community-name.py
@@ -0,0 +1,77 @@
+#
+# Notification Receiver
+#
+# Receive SNMP TRAP/INFORM messages with the following options:
+#
+# * SNMPv1/SNMPv2c
+# * with any SNMP community matching regexp '.*love.*'
+# * over IPv4/UDP, listening at 127.0.0.1:162
+# * print received data on stdout
+#
+# Either of the following Net-SNMP's commands will send notifications to this
+# receiver:
+#
+# $ snmptrap -v1 -c rollover 127.0.0.1 1.3.6.1.4.1.20408.4.1.1.2 127.0.0.1 1 1 123 1.3.6.1.2.1.1.1.0 s test
+# $ snmpinform -v2c -c glove 127.0.0.1 123 1.3.6.1.6.3.1.1.5.1
+#
+from pysnmp.entity import engine, config
+from pysnmp.carrier.asynsock.dgram import udp
+from pysnmp.entity.rfc3413 import ntfrcv
+from pysnmp.proto.api import v2c
+import re
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+# Register a callback to be invoked at specified execution point of
+# SNMP Engine and passed local variables at execution point's local scope.
+# If at this execution point passed variables are modified, their new
+# values will be propagated back and used by SNMP Engine for securityName
+# selection.
+def requestObserver(snmpEngine, execpoint, variables, cbCtx):
+ if re.match('.*love.*', str(variables['communityName'])):
+ print('Rewriting communityName \'%s\' from %s into \'public\'' % (variables['communityName'], ':'.join([str(x) for x in variables['transportInformation'][1]])))
+ variables['communityName'] = variables['communityName'].clone('public')
+
+snmpEngine.observer.registerObserver(
+ requestObserver,
+ 'rfc2576.processIncomingMsg:writable'
+)
+
+# Transport setup
+
+# UDP over IPv4
+config.addTransport(
+ snmpEngine,
+ udp.domainName,
+ udp.UdpTransport().openServerMode(('127.0.0.1', 162))
+)
+
+# 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):
+ print('Notification from ContextEngineId "%s", ContextName "%s"' % (
+ 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)
+
+snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
+
+# Run I/O dispatcher which would receive queries and send confirmations
+try:
+ snmpEngine.transportDispatcher.runDispatcher()
+except:
+ snmpEngine.transportDispatcher.closeDispatcher()
+ raise