summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelie <elie>2013-05-05 11:16:48 +0000
committerelie <elie>2013-05-05 11:16:48 +0000
commit0504f5440037ae48dac865e3f59f762aeb2f9715 (patch)
treef2af2eca6da317a2b3f38bf6314a39a09781783c
parenta6bb3d327278d4a981b685aa01e02e053eb56115 (diff)
downloadpysnmp-0504f5440037ae48dac865e3f59f762aeb2f9715.tar.gz
SNMP Proxy example apps separated into a larger set of more specialized ones
-rw-r--r--CHANGES2
-rw-r--r--examples/v3arch/proxy/cmd/udp6-to-udp4-conversion.py140
-rw-r--r--examples/v3arch/proxy/cmd/v2c-to-v1-conversion.py138
-rw-r--r--examples/v3arch/proxy/cmd/v2c-to-v3-conversion.py (renamed from examples/v3arch/proxy/1to3.py)33
-rw-r--r--examples/v3arch/proxy/cmd/v3-to-v2c-conversion.py142
-rw-r--r--examples/v3arch/proxy/cmdproxy.py109
6 files changed, 436 insertions, 128 deletions
diff --git a/CHANGES b/CHANGES
index 52f6bf7..2fd433f 100644
--- a/CHANGES
+++ b/CHANGES
@@ -5,6 +5,8 @@ Revision 4.2.5rc1
(http://opensource.org/licenses/BSD-2-Clause).
- A dozen of lightweight Twisted-based example scripts replaced more
complex example implementations used previously.
+- SNMP Proxy example apps separated into a larger set of more specialized
+ ones.
- Packet-level SNMP API (pysnmp.proto.api) getErrorIndex() method can now
be instructed to ignore portentially malformed errorIndex SNMP packet
value what sometimes happens with buggy SNMP implementations.
diff --git a/examples/v3arch/proxy/cmd/udp6-to-udp4-conversion.py b/examples/v3arch/proxy/cmd/udp6-to-udp4-conversion.py
new file mode 100644
index 0000000..dd776db
--- /dev/null
+++ b/examples/v3arch/proxy/cmd/udp6-to-udp4-conversion.py
@@ -0,0 +1,140 @@
+#
+# SNMP Command Proxy example
+#
+# Act as a local SNMPv1/v2c Agent listening on a UDP/IPv6 transport, relay
+# messages to distant SNMPv1/2c Agent over UDP/IPv4 transport:
+# with local SNMPv2c community 'public'
+# local Agent listening at [::1]:161
+# remote SNMPv2c community 'public'
+# remote Agent listening at 195.218.195.228:161
+#
+# This script can be queried with the following Net-SNMP command:
+#
+# $ snmpget -v2c -c public udp6:[::1]:161 sysDescr.0
+#
+# due to proxy, it is equivalent to
+#
+# $ snmpget -v2c -c public 195.218.195.228:161 sysDescr.0
+#
+from pysnmp.carrier.asynsock.dgram import udp, udp6
+from pysnmp.entity import engine, config
+from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
+from pysnmp.proto.api import v2c
+from pysnmp import error
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+#
+# Transport setup
+#
+
+# Agent section
+
+# UDP over IPv6
+config.addSocketTransport(
+ snmpEngine,
+ udp6.domainName,
+ udp6.Udp6Transport().openServerMode(('::1', 161))
+)
+
+# Manager section
+
+# UDP over IPv4
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName,
+ udp.UdpTransport().openClientMode()
+)
+
+#
+# SNMPv1/2c setup (Agent role)
+#
+
+# SecurityName <-> CommunityName mapping
+config.addV1System(snmpEngine, '1-my-area', 'public')
+
+#
+# SNMPv1/v2c setup (Manager role)
+#
+# Here we configure securityName lexicographically lesser than '1-my-area'
+# to let it match first in snmpCommunityTable on response processing.
+#
+
+config.addV1System(snmpEngine, '0-distant-area', 'public',
+ transportTag='remote')
+
+#
+# Transport target used by Manager
+#
+
+config.addTargetParams(
+ snmpEngine, 'distant-agent-auth', '0-distant-area', 'noAuthNoPriv', 1
+)
+config.addTargetAddr(
+ snmpEngine, 'distant-agent',
+ udp.domainName, ('195.218.195.228', 161),
+ 'distant-agent-auth', retryCount=0, tagList='remote'
+)
+
+# Default SNMP context
+config.addContext(snmpEngine, '')
+
+class CommandResponder(cmdrsp.CommandResponderBase):
+ cmdGenMap = {
+ v2c.GetRequestPDU.tagSet: cmdgen.GetCommandGenerator(),
+ v2c.SetRequestPDU.tagSet: cmdgen.SetCommandGenerator(),
+ v2c.GetNextRequestPDU.tagSet: cmdgen.NextCommandGeneratorSingleRun(),
+ v2c.GetBulkRequestPDU.tagSet: cmdgen.BulkCommandGeneratorSingleRun()
+ }
+ pduTypes = cmdGenMap.keys() # This app will handle these PDUs
+
+ # SNMP request relay
+ def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
+ PDU, acInfo):
+ cbCtx = snmpEngine, stateReference
+ varBinds = v2c.apiPDU.getVarBinds(PDU)
+ try:
+ if PDU.tagSet == v2c.GetBulkRequestPDU.tagSet:
+ self.cmdGenMap[PDU.tagSet].sendReq(
+ snmpEngine, 'distant-agent',
+ v2c.apiBulkPDU.getNonRepeaters(PDU),
+ v2c.apiBulkPDU.getMaxRepetitions(PDU),
+ varBinds,
+ self.handleResponse, cbCtx
+ )
+ elif PDU.tagSet in self.cmdGenMap:
+ self.cmdGenMap[PDU.tagSet].sendReq(
+ snmpEngine, 'distant-agent', varBinds,
+ self.handleResponse, cbCtx
+ )
+ except error.PySnmpError:
+ self.handleResponse(
+ stateReference, 'error', 0, 0, varBinds, cbCtx
+ )
+
+ # SNMP response relay
+ def handleResponse(self, sendRequestHandle, errorIndication,
+ errorStatus, errorIndex, varBinds, cbCtx):
+ if errorIndication:
+ errorStatus = 5
+ errorIndex = 0
+ varBinds = ()
+
+ snmpEngine, stateReference = cbCtx
+
+ self.sendRsp(
+ snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ )
+
+CommandResponder(snmpEngine, context.SnmpContext(snmpEngine))
+
+snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
+
+# Run I/O dispatcher which would receive queries and send responses
+try:
+ snmpEngine.transportDispatcher.runDispatcher()
+except:
+ snmpEngine.transportDispatcher.closeDispatcher()
+ raise
diff --git a/examples/v3arch/proxy/cmd/v2c-to-v1-conversion.py b/examples/v3arch/proxy/cmd/v2c-to-v1-conversion.py
new file mode 100644
index 0000000..6e456b7
--- /dev/null
+++ b/examples/v3arch/proxy/cmd/v2c-to-v1-conversion.py
@@ -0,0 +1,138 @@
+#
+# SNMP Command Proxy example
+#
+# Act as a local SNMPv2c Agent, relay messages to distant SNMPv1 Agent:
+# over IPv4/UDP
+# with local SNMPv2c community public
+# local Agent listening at 127.0.0.1:161
+# remote SNMPv1, community public
+# remote Agent listening at 195.218.195.228:161
+#
+# This script can be queried with the following Net-SNMP command:
+#
+# $ snmpbulkwalk -v2c -c public -ObentU 127.0.0.1:161 system
+#
+# due to proxy, it is equivalent to
+#
+# $ snmpwalk -v1 -c public 195.218.195.228:161 system
+#
+from pysnmp.carrier.asynsock.dgram import udp
+from pysnmp.entity import engine, config
+from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
+from pysnmp.proto.api import v2c
+from pysnmp import error
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+#
+# Transport setup
+#
+
+# Agent section
+
+# UDP over IPv4
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (1,),
+ udp.UdpTransport().openServerMode(('127.0.0.1', 161))
+)
+
+# Manager section
+
+# UDP over IPv4
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (2,),
+ udp.UdpTransport().openClientMode()
+)
+
+#
+# SNMPv2c setup (Agent role)
+#
+
+# SecurityName <-> CommunityName mapping
+config.addV1System(snmpEngine, 'my-area', 'public')
+
+#
+# SNMPv1 setup (Manager role)
+#
+
+# SecurityName <-> CommunityName <-> Transport mapping
+config.addV1System(snmpEngine, 'distant-area', 'public', transportTag='distant')
+
+#
+# Transport target used by Manager
+#
+
+# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1)
+config.addTargetParams(snmpEngine, 'distant-agent-auth', 'distant-area',
+ 'noAuthNoPriv', 0)
+
+config.addTargetAddr(
+ snmpEngine, 'distant-agent',
+ udp.domainName + (2,), ('195.218.195.228', 161),
+ 'distant-agent-auth', retryCount=0, tagList='distant'
+)
+
+# Default SNMP context
+config.addContext(snmpEngine, '')
+
+class CommandResponder(cmdrsp.CommandResponderBase):
+ cmdGenMap = {
+ v2c.GetRequestPDU.tagSet: cmdgen.GetCommandGenerator(),
+ v2c.SetRequestPDU.tagSet: cmdgen.SetCommandGenerator(),
+ v2c.GetNextRequestPDU.tagSet: cmdgen.NextCommandGeneratorSingleRun(),
+ v2c.GetBulkRequestPDU.tagSet: cmdgen.BulkCommandGeneratorSingleRun()
+ }
+ pduTypes = cmdGenMap.keys() # This app will handle these PDUs
+
+ # SNMP request relay
+ def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
+ PDU, acInfo):
+ cbCtx = snmpEngine, stateReference
+ varBinds = v2c.apiPDU.getVarBinds(PDU)
+ try:
+ if PDU.tagSet == v2c.GetBulkRequestPDU.tagSet:
+ self.cmdGenMap[PDU.tagSet].sendReq(
+ snmpEngine, 'distant-agent',
+ v2c.apiBulkPDU.getNonRepeaters(PDU),
+ v2c.apiBulkPDU.getMaxRepetitions(PDU),
+ varBinds,
+ self.handleResponse, cbCtx
+ )
+ elif PDU.tagSet in self.cmdGenMap:
+ self.cmdGenMap[PDU.tagSet].sendReq(
+ snmpEngine, 'distant-agent', varBinds,
+ self.handleResponse, cbCtx
+ )
+ except error.PySnmpError:
+ self.handleResponse(
+ stateReference, 'error', 0, 0, varBinds, cbCtx
+ )
+
+ # SNMP response relay
+ def handleResponse(self, sendRequestHandle, errorIndication,
+ errorStatus, errorIndex, varBinds, cbCtx):
+ if errorIndication:
+ errorStatus = 5
+ errorIndex = 0
+ varBinds = ()
+
+ snmpEngine, stateReference = cbCtx
+
+ self.sendRsp(
+ snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ )
+
+CommandResponder(snmpEngine, context.SnmpContext(snmpEngine))
+
+snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
+
+# Run I/O dispatcher which would receive queries and send responses
+try:
+ snmpEngine.transportDispatcher.runDispatcher()
+except:
+ snmpEngine.transportDispatcher.closeDispatcher()
+ raise
diff --git a/examples/v3arch/proxy/1to3.py b/examples/v3arch/proxy/cmd/v2c-to-v3-conversion.py
index 4cbab94..c79075d 100644
--- a/examples/v3arch/proxy/1to3.py
+++ b/examples/v3arch/proxy/cmd/v2c-to-v3-conversion.py
@@ -1,5 +1,5 @@
#
-# SNMP proxy example
+# SNMP Command Proxy example
#
# Act as a local SNMPv1/v2c Agent, relay messages to distant SNMPv3 Agent:
# over IPv4/UDP
@@ -7,7 +7,6 @@
# local Agent listening at 127.0.0.1:161
# remote SNMPv3 user usr-md5-none, MD5 auth and no privacy protocols
# remote Agent listening at 195.218.195.228:161
-# for an OID in tuple form
#
# This script can be queried with the following Net-SNMP command:
#
@@ -17,18 +16,12 @@
#
# $ snmpget -v3 -l authNoPriv -u usr-md5-none -A authkey1 -ObentU 195.218.195.228:161 1.3.6.1.2.1.1.1.0
#
-from pysnmp.carrier.asynsock.dgram import udp, udp6
+from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
from pysnmp.proto.api import v2c
from pysnmp import error
-# Basic configuration
-remoteTransportDomain = udp.domainName
-remoteTransportAddress = '195.218.195.228', 161
-remoteV3User = 'usr-md5-none'
-remoteV3AuthKey = 'authkey1'
-
# Create SNMP engine with autogenernated engineID and pre-bound
# to socket transport dispatcher
snmpEngine = engine.SnmpEngine()
@@ -51,7 +44,7 @@ config.addSocketTransport(
# UDP over IPv4
config.addSocketTransport(
snmpEngine,
- udp.domainName,
+ udp.domainName + (2,),
udp.UdpTransport().openClientMode()
)
@@ -68,8 +61,7 @@ config.addV1System(snmpEngine, 'my-area', 'public')
# user: usr-md5-none, auth: MD5, priv NONE
config.addV3User(
- snmpEngine, remoteV3User,
- config.usmHMACMD5AuthProtocol, remoteV3AuthKey
+ snmpEngine, 'usr-md5-none', config.usmHMACMD5AuthProtocol, 'authkey1'
)
#
@@ -81,7 +73,7 @@ config.addTargetParams(
)
config.addTargetAddr(
snmpEngine, 'distant-agent',
- remoteTransportDomain, remoteTransportAddress,
+ udp.domainName + (2,), ('195.218.195.228', 161),
'distant-agent-auth', retryCount=0
)
@@ -97,9 +89,10 @@ class CommandResponder(cmdrsp.CommandResponderBase):
}
pduTypes = cmdGenMap.keys() # This app will handle these PDUs
+ # SNMP request relay
def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
PDU, acInfo):
- (acFun, acCtx) = acInfo
+ cbCtx = snmpEngine, stateReference
varBinds = v2c.apiPDU.getVarBinds(PDU)
try:
if PDU.tagSet == v2c.GetBulkRequestPDU.tagSet:
@@ -108,16 +101,19 @@ class CommandResponder(cmdrsp.CommandResponderBase):
v2c.apiBulkPDU.getNonRepeaters(PDU),
v2c.apiBulkPDU.getMaxRepetitions(PDU),
varBinds,
- self.handleResponse, stateReference
+ self.handleResponse, cbCtx
)
elif PDU.tagSet in self.cmdGenMap:
self.cmdGenMap[PDU.tagSet].sendReq(
snmpEngine, 'distant-agent', varBinds,
- self.handleResponse, stateReference
+ self.handleResponse, cbCtx
)
except error.PySnmpError:
- self.sendRsp(snmpEngine, stateReference, 5, 0, varBinds)
+ self.handleResponse(
+ stateReference, 'error', 0, 0, varBinds, cbCtx
+ )
+ # SNMP response relay
def handleResponse(self, sendRequestHandle, errorIndication,
errorStatus, errorIndex, varBinds, cbCtx):
if errorIndication:
@@ -125,13 +121,12 @@ class CommandResponder(cmdrsp.CommandResponderBase):
errorIndex = 0
varBinds = ()
- stateReference = cbCtx
+ snmpEngine, stateReference = cbCtx
self.sendRsp(
snmpEngine, stateReference, errorStatus, errorIndex, varBinds
)
-# Command Responder app registration
CommandResponder(snmpEngine, context.SnmpContext(snmpEngine))
snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
diff --git a/examples/v3arch/proxy/cmd/v3-to-v2c-conversion.py b/examples/v3arch/proxy/cmd/v3-to-v2c-conversion.py
new file mode 100644
index 0000000..4ecc50a
--- /dev/null
+++ b/examples/v3arch/proxy/cmd/v3-to-v2c-conversion.py
@@ -0,0 +1,142 @@
+#
+# SNMP Command Proxy example
+#
+# Act as a local SNMPv3 Agent, relay messages to distant SNMPv1/v2c Agent:
+# over IPv4/UDP
+# with local SNMPv3 user usr-md5-des, MD5 auth and DES privacy protocols
+# local Agent listening at 127.0.0.1:161
+# remote SNMPv1, community public
+# remote Agent listening at 195.218.195.228:161
+#
+# This script can be queried with the following Net-SNMP command:
+#
+# $ snmpget -v3 -l authPriv -u usr-md5-des -A authkey1 -X privkey1 -ObentU 127.0.0.1:161 1.3.6.1.2.1.1.1.0
+#
+# due to proxy, it is equivalent to
+#
+# $ snmpget -v2c -c public 195.218.195.228:161 1.3.6.1.2.1.1.1.0
+#
+from pysnmp.carrier.asynsock.dgram import udp
+from pysnmp.entity import engine, config
+from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
+from pysnmp.proto.api import v2c
+from pysnmp import error
+
+# Create SNMP engine with autogenernated engineID and pre-bound
+# to socket transport dispatcher
+snmpEngine = engine.SnmpEngine()
+
+#
+# Transport setup
+#
+
+# Agent section
+
+# UDP over IPv4
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (1,),
+ udp.UdpTransport().openServerMode(('127.0.0.1', 161))
+)
+
+# Manager section
+
+# UDP over IPv4
+config.addSocketTransport(
+ snmpEngine,
+ udp.domainName + (2,),
+ udp.UdpTransport().openClientMode()
+)
+
+#
+# SNMPv3/USM setup (Agent role)
+#
+
+# user: usr-md5-des, auth: MD5, priv DES
+config.addV3User(
+ snmpEngine, 'usr-md5-des',
+ config.usmHMACMD5AuthProtocol, 'authkey1',
+ config.usmDESPrivProtocol, 'privkey1'
+)
+
+#
+# SNMPv1/2c setup (Manager role)
+#
+
+# SecurityName <-> CommunityName mapping
+config.addV1System(snmpEngine, 'my-area', 'public')
+
+#
+# Transport target used by Manager
+#
+
+# Specify security settings per SecurityName (SNMPv1 - 0, SNMPv2c - 1)
+config.addTargetParams(snmpEngine, 'distant-agent-auth', 'my-area',
+ 'noAuthNoPriv', 0)
+
+config.addTargetAddr(
+ snmpEngine, 'distant-agent',
+ udp.domainName + (2,), ('195.218.195.228', 161),
+ 'distant-agent-auth', retryCount=0
+)
+
+# Default SNMP context
+config.addContext(snmpEngine, '')
+
+class CommandResponder(cmdrsp.CommandResponderBase):
+ cmdGenMap = {
+ v2c.GetRequestPDU.tagSet: cmdgen.GetCommandGenerator(),
+ v2c.SetRequestPDU.tagSet: cmdgen.SetCommandGenerator(),
+ v2c.GetNextRequestPDU.tagSet: cmdgen.NextCommandGeneratorSingleRun(),
+ v2c.GetBulkRequestPDU.tagSet: cmdgen.BulkCommandGeneratorSingleRun()
+ }
+ pduTypes = cmdGenMap.keys() # This app will handle these PDUs
+
+ # SNMP request relay
+ def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
+ PDU, acInfo):
+ cbCtx = snmpEngine, stateReference
+ varBinds = v2c.apiPDU.getVarBinds(PDU)
+ try:
+ if PDU.tagSet == v2c.GetBulkRequestPDU.tagSet:
+ self.cmdGenMap[PDU.tagSet].sendReq(
+ snmpEngine, 'distant-agent',
+ v2c.apiBulkPDU.getNonRepeaters(PDU),
+ v2c.apiBulkPDU.getMaxRepetitions(PDU),
+ varBinds,
+ self.handleResponse, cbCtx
+ )
+ elif PDU.tagSet in self.cmdGenMap:
+ self.cmdGenMap[PDU.tagSet].sendReq(
+ snmpEngine, 'distant-agent', varBinds,
+ self.handleResponse, cbCtx
+ )
+ except error.PySnmpError:
+ self.handleResponse(
+ stateReference, 'error', 0, 0, varBinds, cbCtx
+ )
+
+ # SNMP response relay
+ def handleResponse(self, sendRequestHandle, errorIndication,
+ errorStatus, errorIndex, varBinds, cbCtx):
+ if errorIndication:
+ errorStatus = 5
+ errorIndex = 0
+ varBinds = ()
+
+ snmpEngine, stateReference = cbCtx
+
+ self.sendRsp(
+ snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ )
+
+CommandResponder(snmpEngine, context.SnmpContext(snmpEngine))
+
+snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish
+
+# Run I/O dispatcher which would receive queries and send responses
+try:
+ snmpEngine.transportDispatcher.runDispatcher()
+except:
+ snmpEngine.transportDispatcher.closeDispatcher()
+ raise
diff --git a/examples/v3arch/proxy/cmdproxy.py b/examples/v3arch/proxy/cmdproxy.py
deleted file mode 100644
index 50d7f1a..0000000
--- a/examples/v3arch/proxy/cmdproxy.py
+++ /dev/null
@@ -1,109 +0,0 @@
-#
-# 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.proto.acmod import void
-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 = void.accessModelID
- cmdGen = cmdgen.GetCommandGenerator()
-
- def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
- PDU, acInfo):
- (acFun, acCtx) = acInfo
- 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:
- self.sendRsp(snmpEngine, stateReference, 5, 0, varBinds)
-
- def handleResponse(self, sendRequestHandle, errorIndication,
- errorStatus, errorIndex, varBinds, cbCtx):
- (stateReference, reqVarBinds) = cbCtx
- 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()