summaryrefslogtreecommitdiff
path: root/examples/v1arch
diff options
context:
space:
mode:
authorelie <elie>2011-11-06 20:37:09 +0000
committerelie <elie>2011-11-06 20:37:09 +0000
commitf0406dd01c46230ebbcd4f8c4a47fdbc270e64ef (patch)
treecae924c26bc40e6caafde18e657ab7fbb8794bbe /examples/v1arch
parent6781949a085961ff2eb6f4603a52249e99c4ce7d (diff)
downloadpysnmp-git-f0406dd01c46230ebbcd4f8c4a47fdbc270e64ef.tar.gz
major overhawl aimed at Python 2.4 through 3.2 compatibility
Diffstat (limited to 'examples/v1arch')
-rw-r--r--examples/v1arch/asyncore/agent/cmdrsp/implementing-scalar-mib-objects.py16
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py6
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/getbulk-pull-whole-mib.py82
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py15
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/v2c-set.py6
-rw-r--r--examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py24
6 files changed, 123 insertions, 26 deletions
diff --git a/examples/v1arch/asyncore/agent/cmdrsp/implementing-scalar-mib-objects.py b/examples/v1arch/asyncore/agent/cmdrsp/implementing-scalar-mib-objects.py
index 9b3f992c..d59d9e7c 100644
--- a/examples/v1arch/asyncore/agent/cmdrsp/implementing-scalar-mib-objects.py
+++ b/examples/v1arch/asyncore/agent/cmdrsp/implementing-scalar-mib-objects.py
@@ -7,7 +7,12 @@ import time, bisect
class SysDescr:
name = (1,3,6,1,2,1,1,1,0)
- def __cmp__(self, other): return cmp(self.name, other)
+ def __eq__(self, other): return self.name == other
+ def __ne__(self, other): return self.name != other
+ def __lt__(self, other): return self.name < other
+ def __le__(self, other): return self.name <= other
+ def __gt__(self, other): return self.name > other
+ def __ge__(self, other): return self.name >= other
def __call__(self, protoVer):
return api.protoModules[protoVer].OctetString(
'PySNMP example command responder'
@@ -16,7 +21,12 @@ class SysDescr:
class Uptime:
name = (1,3,6,1,2,1,1,3,0)
birthday = time.time()
- def __cmp__(self, other): return cmp(self.name, other)
+ def __eq__(self, other): return self.name == other
+ def __ne__(self, other): return self.name != other
+ def __lt__(self, other): return self.name < other
+ def __le__(self, other): return self.name <= other
+ def __gt__(self, other): return self.name > other
+ def __ge__(self, other): return self.name >= other
def __call__(self, protoVer):
return api.protoModules[protoVer].TimeTicks(
(time.time()-self.birthday)*100
@@ -36,7 +46,7 @@ def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg):
if msgVer in api.protoModules:
pMod = api.protoModules[msgVer]
else:
- print 'Unsupported SNMP version %s' % msgVer
+ print('Unsupported SNMP version %s' % msgVer)
return
reqMsg, wholeMsg = decoder.decode(
wholeMsg, asn1Spec=pMod.Message(),
diff --git a/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py b/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py
index 1189659a..2ba0fea9 100644
--- a/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py
+++ b/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py
@@ -24,7 +24,7 @@ pMod.apiMessage.setPDU(reqMsg, reqPDU)
def cbTimerFun(timeNow, startedAt=time()):
if timeNow - startedAt > 3:
- raise "Request timed out"
+ raise Exception("Request timed out")
def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
wholeMsg, reqPDU=reqPDU):
@@ -36,10 +36,10 @@ def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
# Check for SNMP errors reported
errorStatus = pMod.apiPDU.getErrorStatus(rspPDU)
if errorStatus:
- print errorStatus.prettyPrint()
+ print(errorStatus.prettyPrint())
else:
for oid, val in pMod.apiPDU.getVarBinds(rspPDU):
- print '%s = %s' % (oid.prettyPrint(), val.prettyPrint())
+ print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
transportDispatcher.jobFinished(1)
return wholeMsg
diff --git a/examples/v1arch/asyncore/manager/cmdgen/getbulk-pull-whole-mib.py b/examples/v1arch/asyncore/manager/cmdgen/getbulk-pull-whole-mib.py
new file mode 100644
index 00000000..1e708213
--- /dev/null
+++ b/examples/v1arch/asyncore/manager/cmdgen/getbulk-pull-whole-mib.py
@@ -0,0 +1,82 @@
+# GETBULK Command Generator (SNMPv2c only)
+from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
+from pysnmp.carrier.asynsock.dgram import udp
+from pyasn1.codec.ber import encoder, decoder
+from pysnmp.proto.api import v2c
+from time import time
+
+# SNMP table header
+headVars = [ v2c.ObjectIdentifier((1,3,6)) ]
+
+# Build PDU
+reqPDU = v2c.GetBulkRequestPDU()
+v2c.apiBulkPDU.setDefaults(reqPDU)
+v2c.apiBulkPDU.setNonRepeaters(reqPDU, 0)
+v2c.apiBulkPDU.setMaxRepetitions(reqPDU, 25)
+v2c.apiBulkPDU.setVarBinds(reqPDU, [ (x, v2c.null) for x in headVars ])
+
+# Build message
+reqMsg = v2c.Message()
+v2c.apiMessage.setDefaults(reqMsg)
+v2c.apiMessage.setCommunity(reqMsg, 'public')
+v2c.apiMessage.setPDU(reqMsg, reqPDU)
+
+startedAt = time()
+
+def cbTimerFun(timeNow):
+ if timeNow - startedAt > 3:
+ raise Exception("Request timed out")
+
+def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
+ wholeMsg, reqPDU=reqPDU, headVars=headVars):
+ while wholeMsg:
+ rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=v2c.Message())
+ rspPDU = v2c.apiMessage.getPDU(rspMsg)
+ # Match response to request
+ if v2c.apiBulkPDU.getRequestID(reqPDU)==v2c.apiBulkPDU.getRequestID(rspPDU):
+ # Check for SNMP errors reported
+ errorStatus = v2c.apiBulkPDU.getErrorStatus(rspPDU)
+ if errorStatus and errorStatus != 2:
+ raise Exception(errorStatus)
+ # Format var-binds table
+ varBindTable = v2c.apiBulkPDU.getVarBindTable(reqPDU, rspPDU)
+ # Report SNMP table
+ for tableRow in varBindTable:
+ for name, val in tableRow:
+ print('from: %s, %s = %s' % (
+ transportAddress, name.prettyPrint(), val.prettyPrint()
+ )
+ )
+ # Stop on EOM
+ for oid, val in varBindTable[-1]:
+ if not isinstance(val, v2c.Null):
+ break
+ else:
+ transportDispatcher.jobFinished(1)
+
+ # Generate request for next row
+ v2c.apiBulkPDU.setVarBinds(
+ reqPDU, [ (x, v2c.null) for x,y in varBindTable[-1] ]
+ )
+ v2c.apiBulkPDU.setRequestID(reqPDU, v2c.getNextRequestID())
+ transportDispatcher.sendMessage(
+ encoder.encode(reqMsg), transportDomain, transportAddress
+ )
+ global startedAt
+ if time() - startedAt > 3:
+ raise Exception('Request timed out')
+ startedAt = time()
+ return wholeMsg
+
+transportDispatcher = AsynsockDispatcher()
+transportDispatcher.registerTransport(
+ udp.domainName, udp.UdpSocketTransport().openClientMode()
+ )
+transportDispatcher.registerRecvCbFun(cbRecvFun)
+transportDispatcher.registerTimerCbFun(cbTimerFun)
+transportDispatcher.sendMessage(
+ encoder.encode(reqMsg), udp.domainName, ('localhost', 161)
+ )
+transportDispatcher.jobStarted(1)
+transportDispatcher.runDispatcher()
+transportDispatcher.closeDispatcher()
diff --git a/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py b/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py
index 14dcebdd..27e1493c 100644
--- a/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py
+++ b/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py
@@ -15,9 +15,7 @@ headVars = [ pMod.ObjectIdentifier((1,3,6)) ]
# Build PDU
reqPDU = pMod.GetNextRequestPDU()
pMod.apiPDU.setDefaults(reqPDU)
-pMod.apiPDU.setVarBinds(
- reqPDU, map(lambda x, pMod=pMod: (x, pMod.Null('')), headVars)
- )
+pMod.apiPDU.setVarBinds(reqPDU, [ (x, pMod.null) for x in headVars ])
# Build message
reqMsg = pMod.Message()
@@ -29,7 +27,7 @@ startedAt = time()
def cbTimerFun(timeNow):
if timeNow - startedAt > 3:
- raise "Request timed out"
+ raise Exception("Request timed out")
def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
wholeMsg, reqPDU=reqPDU, headVars=headVars):
@@ -41,15 +39,16 @@ def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
# Check for SNMP errors reported
errorStatus = pMod.apiPDU.getErrorStatus(rspPDU)
if errorStatus and errorStatus != 2:
- raise errorStatus
+ raise Exception(errorStatus)
# Format var-binds table
varBindTable = pMod.apiPDU.getVarBindTable(reqPDU, rspPDU)
# Report SNMP table
for tableRow in varBindTable:
for name, val in tableRow:
- print 'from: %s, %s = %s' % (
+ print('from: %s, %s = %s' % (
transportAddress, name.prettyPrint(), val.prettyPrint()
)
+ )
# Stop on EOM
for oid, val in varBindTable[-1]:
if not isinstance(val, pMod.Null):
@@ -59,7 +58,7 @@ def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
# Generate request for next row
pMod.apiPDU.setVarBinds(
- reqPDU, map(lambda (x,y),n=pMod.Null(''): (x,n), varBindTable[-1])
+ reqPDU, [ (x, pMod.null) for x,y in varBindTable[-1] ]
)
pMod.apiPDU.setRequestID(reqPDU, pMod.getNextRequestID())
transportDispatcher.sendMessage(
@@ -67,7 +66,7 @@ def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
)
global startedAt
if time() - startedAt > 3:
- raise 'Request timed out'
+ raise Exception('Request timed out')
startedAt = time()
return wholeMsg
diff --git a/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py b/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py
index 0d27be5b..eec3731a 100644
--- a/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py
+++ b/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py
@@ -26,7 +26,7 @@ pMod.apiMessage.setPDU(reqMsg, reqPDU)
def cbTimerFun(timeNow, startedAt=time()):
if timeNow - startedAt > 3:
- raise "Request timed out"
+ raise Exception("Request timed out")
def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
wholeMsg, reqPDU=reqPDU):
@@ -38,10 +38,10 @@ def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
# Check for SNMP errors reported
errorStatus = pMod.apiPDU.getErrorStatus(rspPDU)
if errorStatus:
- print errorStatus.prettyPrint()
+ print(errorStatus.prettyPrint())
else:
for oid, val in pMod.apiPDU.getVarBinds(rspPDU):
- print '%s = %s' (oid.prettyPrint(), val.prettyPrint())
+ print('%s = %s' (oid.prettyPrint(), val.prettyPrint()))
transportDispatcher.jobFinished(1)
return wholeMsg
diff --git a/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py b/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py
index a38c14cd..62f81474 100644
--- a/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py
+++ b/examples/v1arch/asyncore/manager/ntfrcv/listen-on-ipv4-and-ipv6-interfaces.py
@@ -10,38 +10,44 @@ def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg):
if msgVer in api.protoModules:
pMod = api.protoModules[msgVer]
else:
- print 'Unsupported SNMP version %s' % msgVer
+ print('Unsupported SNMP version %s' % msgVer)
return
reqMsg, wholeMsg = decoder.decode(
wholeMsg, asn1Spec=pMod.Message(),
)
- print 'Notification message from %s:%s: ' % (
+ print('Notification message from %s:%s: ' % (
transportDomain, transportAddress
)
+ )
reqPDU = pMod.apiMessage.getPDU(reqMsg)
if reqPDU.isSameTypeWith(pMod.TrapPDU()):
if msgVer == api.protoVersion1:
- print 'Enterprise: %s' % (
+ print('Enterprise: %s' % (
pMod.apiTrapPDU.getEnterprise(reqPDU).prettyPrint()
)
- print 'Agent Address: %s' % (
+ )
+ print('Agent Address: %s' % (
pMod.apiTrapPDU.getAgentAddr(reqPDU).prettyPrint()
)
- print 'Generic Trap: %s' % (
+ )
+ print('Generic Trap: %s' % (
pMod.apiTrapPDU.getGenericTrap(reqPDU).prettyPrint()
)
- print 'Specific Trap: %s' % (
+ )
+ print('Specific Trap: %s' % (
pMod.apiTrapPDU.getSpecificTrap(reqPDU).prettyPrint()
)
- print 'Uptime: %s' % (
+ )
+ print('Uptime: %s' % (
pMod.apiTrapPDU.getTimeStamp(reqPDU).prettyPrint()
)
+ )
varBinds = pMod.apiTrapPDU.getVarBindList(reqPDU)
else:
varBinds = pMod.apiPDU.getVarBindList(reqPDU)
- print 'Var-binds:'
+ print('Var-binds:')
for oid, val in varBinds:
- print '%s = %s' % (oid.prettyPrint(), val.prettyPrint())
+ print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
return wholeMsg
transportDispatcher = AsynsockDispatcher()