summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2010-12-01 18:22:49 +0000
committerelie <elie>2010-12-01 18:22:49 +0000
commit823ae420930015102052f9598a9f9e7b5d90cb6e (patch)
tree0b28819658ffb5d2f8c935b67be522bc786ca963
parent7f1991199e5162bcd70cd7520dc256384a854f6c (diff)
downloadpysnmp-823ae420930015102052f9598a9f9e7b5d90cb6e.tar.gz
SNMP proxy (for GET request type) added
-rw-r--r--CHANGES1
-rw-r--r--examples/v3arch/proxy/cmdproxy.py107
2 files changed, 108 insertions, 0 deletions
diff --git a/CHANGES b/CHANGES
index 091c230..d80c3cc 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,7 @@
Revision 4.1.15a
----------------
+- SNMP Proxy example added.
- End-of-MIB condition detection reworked what caused backward
incompatibility at v1arch GETNEXT API. Previous pysnmp versions
used value = None in var-binds as returned by getVarBindTable()
diff --git a/examples/v3arch/proxy/cmdproxy.py b/examples/v3arch/proxy/cmdproxy.py
new file mode 100644
index 0000000..1d7233d
--- /dev/null
+++ b/examples/v3arch/proxy/cmdproxy.py
@@ -0,0 +1,107 @@
+#
+# SNMP proxy example
+#
+# Relay SNMP messages between Managers and Agents using any combinations
+# of SNMP protocol versions for both up and downstream packets.
+#
+# To query SNMPv3 Manager over SNMPv2c through this Proxy use:
+# snmpget -v2c -c tgt-v3-1 localhost:1161 sysDescr.0
+# or SNMPv2c Manager over SNMPv3:
+# snmpget -v3 -u test-user -lauthPriv -A authkey1 -X privkey1 -n tgt-v2c-1 localhost:1161 sysDescr.0
+# there are four combinations in total. ;)
+#
+from pysnmp.entity import engine, config
+from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
+from pysnmp.proto.api import v2c
+from pysnmp.carrier.asynsock.dgram import udp
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+# Setup UDP over IPv4 transport endpoints
+
+# Agent will listen here
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (1,), # use transport domain's sub-name
+ udp.UdpTransport().openServerMode(('127.0.0.1', 1161))
+ )
+
+# Manager will send packets there
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (2,), # use transport domain's sub-name
+ udp.UdpTransport().openClientMode()
+ )
+
+# SNMP credentials used by Manager
+
+# v1/2 setup
+config.addV1System(snmpEngine, 'dest-cmt', 'public')
+
+# v3 setup
+config.addV3User(
+ snmpEngine, 'test-user',
+ config.usmHMACMD5AuthProtocol, 'authkey1',
+ config.usmDESPrivProtocol, 'privkey1'
+# config.usmAesCfb128Protocol, 'privkey1'
+ )
+
+# Transport targets used by Manager
+
+# Target 1, SNMPv3 setup
+config.addTargetParams(snmpEngine, 'v3-dest-1', 'test-user', 'authPriv')
+config.addTargetAddr(
+ snmpEngine, 'tgt-v3-1', config.snmpUDPDomain + (2,),
+ ('127.0.0.1', 161), 'v3-dest-1'
+ )
+# This is to map community to context name in incoming messages
+config.addV1System(snmpEngine, 'v2c-src-A', 'tgt-v3-1', contextName='tgt-v3-1')
+
+# Target 1, SNMPv2c setup
+config.addTargetParams(snmpEngine, 'v2c-dest-1', 'dest-cmt', 'noAuthNoPriv', 1)
+config.addTargetAddr(
+ snmpEngine, 'tgt-v2c-1', config.snmpUDPDomain + (2,),
+ ('127.0.0.1', 161), 'v2c-dest-1'
+ )
+# This is to map community to context name in incoming messages
+config.addV1System(snmpEngine, 'v2c-src-B', 'tgt-v2c-1', contextName='tgt-v2c-1')
+
+# Default SNMP context
+config.addContext(snmpEngine, '')
+snmpContext = context.SnmpContext(snmpEngine)
+
+
+class GetCommandProxy(cmdrsp.GetCommandResponder):
+ acmID = 0 # void access control method
+ cmdGen = cmdgen.GetCommandGenerator()
+
+ def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
+ PDU, (acFun, acCtx)):
+ varBinds = v2c.apiPDU.getVarBinds(PDU)
+ try:
+ # The trick here is to use contextName as SNMP Manager target name
+ self.cmdGen.sendReq(
+ snmpEngine, contextName, varBinds,
+ self.handleResponse, (stateReference, varBinds)
+ )
+ except Exception, why:
+ self.sendRsp(snmpEngine, stateReference, 5, 0, varBinds)
+
+ def handleResponse(self, sendRequestHandle, errorIndication,
+ errorStatus, errorIndex, varBinds,
+ (stateReference, reqVarBinds)):
+ if errorIndication:
+ errorStatus = 5
+ varBinds = reqVarBinds
+
+ self.sendRsp(
+ snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ )
+
+# Apps registration
+GetCommandProxy(snmpEngine, snmpContext)
+
+snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
+snmpEngine.transportDispatcher.runDispatcher()