summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/v3arch/asyncore/proxy/command/udp6-to-udp4-conversion.py51
-rw-r--r--examples/v3arch/asyncore/proxy/command/v2c-to-v1-conversion.py49
-rw-r--r--examples/v3arch/asyncore/proxy/command/v2c-to-v3-conversion.py49
-rw-r--r--examples/v3arch/asyncore/proxy/command/v3-to-v2c-conversion.py49
4 files changed, 93 insertions, 105 deletions
diff --git a/examples/v3arch/asyncore/proxy/command/udp6-to-udp4-conversion.py b/examples/v3arch/asyncore/proxy/command/udp6-to-udp4-conversion.py
index 490f43f4..96947c7a 100644
--- a/examples/v3arch/asyncore/proxy/command/udp6-to-udp4-conversion.py
+++ b/examples/v3arch/asyncore/proxy/command/udp6-to-udp4-conversion.py
@@ -16,6 +16,11 @@
#
# $ snmpget -v2c -c public 195.218.195.228:161 sysDescr.0
#
+# Warning: for production operation you would need to modify this script
+# so that it will re-map possible duplicate request-ID values, coming in
+# initial request PDUs from different Managers, into unique values to
+# avoid sending duplicate request-IDs to Agents.
+#
from pysnmp.carrier.asynsock.dgram import udp, udp6
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
@@ -93,39 +98,31 @@ class CommandResponder(cmdrsp.CommandResponderBase):
# SNMP request relay
def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
PDU, acInfo):
- cbCtx = snmpEngine, stateReference
- varBinds = v2c.apiPDU.getVarBinds(PDU)
+ cbCtx = stateReference, PDU
+ contextEngineId = None # address authoritative SNMP Engine
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
- )
+ self.cmdGenMap[PDU.tagSet].sendPdu(
+ snmpEngine, 'distant-agent',
+ contextEngineId, contextName,
+ PDU,
+ self.handleResponsePdu, cbCtx
+ )
except error.PySnmpError:
- self.handleResponse(
- stateReference, 'error', 0, 0, varBinds, cbCtx
+ self.handleResponsePdu(
+ stateReference, 'error', None, cbCtx
)
-
+
# SNMP response relay
- def handleResponse(self, sendRequestHandle, errorIndication,
- errorStatus, errorIndex, varBinds, cbCtx):
- if errorIndication:
- errorStatus = 5
- errorIndex = 0
- varBinds = ()
+ def handleResponsePdu(self, snmpEngine, sendRequestHandle,
+ errorIndication, PDU, cbCtx):
+ stateReference, reqPDU = cbCtx
- snmpEngine, stateReference = cbCtx
+ if errorIndication:
+ PDU = v2c.apiPDU.getResponse(reqPDU)
+ PDU.setErrorStatus(PDU, 5)
- self.sendRsp(
- snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ self.sendPdu(
+ snmpEngine, stateReference, PDU
)
self.releaseStateInformation(stateReference)
diff --git a/examples/v3arch/asyncore/proxy/command/v2c-to-v1-conversion.py b/examples/v3arch/asyncore/proxy/command/v2c-to-v1-conversion.py
index fd9dff13..c2b54e05 100644
--- a/examples/v3arch/asyncore/proxy/command/v2c-to-v1-conversion.py
+++ b/examples/v3arch/asyncore/proxy/command/v2c-to-v1-conversion.py
@@ -16,6 +16,11 @@
#
# $ snmpwalk -v1 -c public 195.218.195.228:161 system
#
+# Warning: for production operation you would need to modify this script
+# so that it will re-map possible duplicate request-ID values, coming in
+# initial request PDUs from different Managers, into unique values to
+# avoid sending duplicate request-IDs to Agents.
+#
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
@@ -91,39 +96,31 @@ class CommandResponder(cmdrsp.CommandResponderBase):
# SNMP request relay
def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
PDU, acInfo):
- cbCtx = snmpEngine, stateReference
- varBinds = v2c.apiPDU.getVarBinds(PDU)
+ cbCtx = stateReference, PDU
+ contextEngineId = None # address authoritative SNMP Engine
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
- )
+ self.cmdGenMap[PDU.tagSet].sendPdu(
+ snmpEngine, 'distant-agent',
+ contextEngineId, contextName,
+ PDU,
+ self.handleResponsePdu, cbCtx
+ )
except error.PySnmpError:
- self.handleResponse(
- stateReference, 'error', 0, 0, varBinds, cbCtx
+ self.handleResponsePdu(
+ stateReference, 'error', None, cbCtx
)
# SNMP response relay
- def handleResponse(self, sendRequestHandle, errorIndication,
- errorStatus, errorIndex, varBinds, cbCtx):
- if errorIndication:
- errorStatus = 5
- errorIndex = 0
- varBinds = ()
+ def handleResponsePdu(self, snmpEngine, sendRequestHandle,
+ errorIndication, PDU, cbCtx):
+ stateReference, reqPDU = cbCtx
- snmpEngine, stateReference = cbCtx
+ if errorIndication:
+ PDU = v2c.apiPDU.getResponse(reqPDU)
+ PDU.setErrorStatus(PDU, 5)
- self.sendRsp(
- snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ self.sendPdu(
+ snmpEngine, stateReference, PDU
)
self.releaseStateInformation(stateReference)
diff --git a/examples/v3arch/asyncore/proxy/command/v2c-to-v3-conversion.py b/examples/v3arch/asyncore/proxy/command/v2c-to-v3-conversion.py
index 1ce38ca2..dae1e6b9 100644
--- a/examples/v3arch/asyncore/proxy/command/v2c-to-v3-conversion.py
+++ b/examples/v3arch/asyncore/proxy/command/v2c-to-v3-conversion.py
@@ -16,6 +16,11 @@
#
# $ 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
#
+# Warning: for production operation you would need to modify this script
+# so that it will re-map possible duplicate request-ID values, coming in
+# initial request PDUs from different Managers, into unique values to
+# avoid sending duplicate request-IDs to Agents.
+#
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
@@ -92,39 +97,31 @@ class CommandResponder(cmdrsp.CommandResponderBase):
# SNMP request relay
def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
PDU, acInfo):
- cbCtx = snmpEngine, stateReference
- varBinds = v2c.apiPDU.getVarBinds(PDU)
+ cbCtx = stateReference, PDU
+ contextEngineId = None # address authoritative SNMP Engine
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
- )
+ self.cmdGenMap[PDU.tagSet].sendPdu(
+ snmpEngine, 'distant-agent',
+ contextEngineId, contextName,
+ PDU,
+ self.handleResponsePdu, cbCtx
+ )
except error.PySnmpError:
- self.handleResponse(
- stateReference, 'error', 0, 0, varBinds, cbCtx
+ self.handleResponsePdu(
+ stateReference, 'error', None, cbCtx
)
# SNMP response relay
- def handleResponse(self, sendRequestHandle, errorIndication,
- errorStatus, errorIndex, varBinds, cbCtx):
- if errorIndication:
- errorStatus = 5
- errorIndex = 0
- varBinds = ()
+ def handleResponsePdu(self, snmpEngine, sendRequestHandle,
+ errorIndication, PDU, cbCtx):
+ stateReference, reqPDU = cbCtx
- snmpEngine, stateReference = cbCtx
+ if errorIndication:
+ PDU = v2c.apiPDU.getResponse(reqPDU)
+ PDU.setErrorStatus(PDU, 5)
- self.sendRsp(
- snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ self.sendPdu(
+ snmpEngine, stateReference, PDU
)
self.releaseStateInformation(stateReference)
diff --git a/examples/v3arch/asyncore/proxy/command/v3-to-v2c-conversion.py b/examples/v3arch/asyncore/proxy/command/v3-to-v2c-conversion.py
index e36a9898..a0d86716 100644
--- a/examples/v3arch/asyncore/proxy/command/v3-to-v2c-conversion.py
+++ b/examples/v3arch/asyncore/proxy/command/v3-to-v2c-conversion.py
@@ -16,6 +16,11 @@
#
# $ snmpget -v2c -c public 195.218.195.228:161 1.3.6.1.2.1.1.1.0
#
+# Warning: for production operation you would need to modify this script
+# so that it will re-map possible duplicate request-ID values, coming in
+# initial request PDUs from different Managers, into unique values to
+# avoid sending duplicate request-IDs to Agents.
+#
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, cmdgen, context
@@ -95,39 +100,31 @@ class CommandResponder(cmdrsp.CommandResponderBase):
# SNMP request relay
def handleMgmtOperation(self, snmpEngine, stateReference, contextName,
PDU, acInfo):
- cbCtx = snmpEngine, stateReference
- varBinds = v2c.apiPDU.getVarBinds(PDU)
+ cbCtx = stateReference, PDU
+ contextEngineId = None # address authoritative SNMP Engine
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
- )
+ self.cmdGenMap[PDU.tagSet].sendPdu(
+ snmpEngine, 'distant-agent',
+ contextEngineId, contextName,
+ PDU,
+ self.handleResponsePdu, cbCtx
+ )
except error.PySnmpError:
- self.handleResponse(
- stateReference, 'error', 0, 0, varBinds, cbCtx
+ self.handleResponsePdu(
+ stateReference, 'error', None, cbCtx
)
# SNMP response relay
- def handleResponse(self, sendRequestHandle, errorIndication,
- errorStatus, errorIndex, varBinds, cbCtx):
- if errorIndication:
- errorStatus = 5
- errorIndex = 0
- varBinds = ()
+ def handleResponsePdu(self, snmpEngine, sendRequestHandle,
+ errorIndication, PDU, cbCtx):
+ stateReference, reqPDU = cbCtx
- snmpEngine, stateReference = cbCtx
+ if errorIndication:
+ PDU = v2c.apiPDU.getResponse(reqPDU)
+ PDU.setErrorStatus(PDU, 5)
- self.sendRsp(
- snmpEngine, stateReference, errorStatus, errorIndex, varBinds
+ self.sendPdu(
+ snmpEngine, stateReference, PDU
)
self.releaseStateInformation(stateReference)