diff options
author | elie <elie> | 2013-05-03 12:19:57 +0000 |
---|---|---|
committer | elie <elie> | 2013-05-03 12:19:57 +0000 |
commit | 9e08e0b10528c62da67a438f633a93754265c4d1 (patch) | |
tree | 15bc5944644fa2a773bda2859f485e9ed918dfeb /examples | |
parent | 4de9e314b3591a2643755afb9f4b923c564becfb (diff) | |
download | pysnmp-9e08e0b10528c62da67a438f633a93754265c4d1.tar.gz |
example improved
Diffstat (limited to 'examples')
-rw-r--r-- | examples/v3arch/proxy/1to3.py | 54 |
1 files changed, 29 insertions, 25 deletions
diff --git a/examples/v3arch/proxy/1to3.py b/examples/v3arch/proxy/1to3.py index f0a39f8..4cbab94 100644 --- a/examples/v3arch/proxy/1to3.py +++ b/examples/v3arch/proxy/1to3.py @@ -1,24 +1,33 @@ # # SNMP proxy example # -# Act as a local SNMPv1/v2c Agent, relay messages to distant SNMPv3 Manager. +# Act as a local SNMPv1/v2c Agent, relay messages to distant SNMPv3 Agent: +# over IPv4/UDP +# with local SNMPv2c community 'public' +# 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: +# +# $ snmpget -v2c -c public 127.0.0.1:161 1.3.6.1.2.1.1.1.0 +# +# due to proxy, it is equivalent to +# +# $ 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 socket import gethostbyname 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 -#from pysnmp import debug # Basic configuration remoteTransportDomain = udp.domainName -remoteTransportAddress = gethostbyname('test.net-snmp.org'), 161 -remoteV3User = 'MD5User' -remoteV3AuthKey = 'The Net-SNMP Demo Password' - -# Optional debugging ('all' enables full debugging) -#debug.setLogger(debug.Debug('io', 'dsp', 'msgproc', 'secmod', 'app')) +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 @@ -37,13 +46,6 @@ config.addSocketTransport( udp.UdpTransport().openServerMode(('127.0.0.1', 161)) ) -# UDP over IPv6 -config.addSocketTransport( - snmpEngine, - udp6.domainName + (1,), - udp6.Udp6Transport().openServerMode(('::1', 161)) - ) - # Manager section # UDP over IPv4 @@ -75,22 +77,24 @@ config.addV3User( # config.addTargetParams( - snmpEngine, 'distant-agent', 'MD5User', 'authNoPriv' + snmpEngine, 'distant-agent-auth', 'usr-md5-none', 'authNoPriv' ) config.addTargetAddr( - snmpEngine, 'net-snmp-agent', + snmpEngine, 'distant-agent', remoteTransportDomain, remoteTransportAddress, - 'distant-agent', retryCount=0 + '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() } + 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 def handleMgmtOperation(self, snmpEngine, stateReference, contextName, @@ -100,7 +104,7 @@ class CommandResponder(cmdrsp.CommandResponderBase): try: if PDU.tagSet == v2c.GetBulkRequestPDU.tagSet: self.cmdGenMap[PDU.tagSet].sendReq( - snmpEngine, 'net-snmp-agent', + snmpEngine, 'distant-agent', v2c.apiBulkPDU.getNonRepeaters(PDU), v2c.apiBulkPDU.getMaxRepetitions(PDU), varBinds, @@ -108,7 +112,7 @@ class CommandResponder(cmdrsp.CommandResponderBase): ) elif PDU.tagSet in self.cmdGenMap: self.cmdGenMap[PDU.tagSet].sendReq( - snmpEngine, 'net-snmp-agent', varBinds, + snmpEngine, 'distant-agent', varBinds, self.handleResponse, stateReference ) except error.PySnmpError: |