summaryrefslogtreecommitdiff
path: root/examples/v1arch/asyncore
diff options
context:
space:
mode:
authorelie <elie>2004-11-05 19:26:44 +0000
committerelie <elie>2004-11-05 19:26:44 +0000
commitf63cb1e2179b87d38f3f9bd79b0549caef311164 (patch)
tree3b383703debd7bc9e7c3a974944b2cda12b85e20 /examples/v1arch/asyncore
parente9c1d4e14bec03cb68980bcaf94fbbcb9a8ef30f (diff)
downloadpysnmp-git-f63cb1e2179b87d38f3f9bd79b0549caef311164.tar.gz
legacy examples moved under v1arch/
Diffstat (limited to 'examples/v1arch/asyncore')
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py43
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py71
-rw-r--r--examples/v1arch/asyncore/manager/cmdgen/v2c-set.py46
3 files changed, 160 insertions, 0 deletions
diff --git a/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py b/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py
new file mode 100644
index 00000000..f4a518f4
--- /dev/null
+++ b/examples/v1arch/asyncore/manager/cmdgen/fetch-scalar-value.py
@@ -0,0 +1,43 @@
+"""Command Generator Application (GET)"""
+from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
+from pysnmp.carrier.asynsock.dgram.udp import UdpSocketTransport
+from pysnmp.proto.api import alpha
+from time import time
+
+# Protocol version to use
+ver = alpha.protoVersions[alpha.protoVersionId1]
+
+# Build message
+req = ver.Message()
+req.apiAlphaSetCommunity('public')
+
+# Build PDU
+req.apiAlphaSetPdu(ver.GetRequestPdu())
+req.apiAlphaGetPdu().apiAlphaSetVarBindList(
+ ((1,3,6,1,2,1,1,1,0), ver.Null()), ((1,3,6,1,2,1,1,2,0), ver.Null())
+ )
+
+def cbTimerFun(timeNow, startedAt=time()):
+ if timeNow - startedAt > 3:
+ raise "Request timed out"
+
+def cbRecvFun(tspDsp, transportDomain, transportAddress, wholeMsg, req=req):
+ rsp = ver.Message()
+ while wholeMsg:
+ wholeMsg = rsp.berDecode(wholeMsg)
+ # Make sure this is a response to this request
+ if req.apiAlphaMatch(rsp):
+ errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus()
+ if errorStatus:
+ print 'Error: ', errorStatus
+ else:
+ for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList():
+ print varBind.apiAlphaGetOidVal()
+ tspDsp.doDispatchFlag = 0
+ return wholeMsg
+
+dsp = AsynsockDispatcher(udp=UdpSocketTransport().openClientMode())
+dsp.registerRecvCbFun(cbRecvFun)
+dsp.registerTimerCbFun(cbTimerFun)
+dsp.sendMessage(req.berEncode(), 'udp', ('localhost', 1161)) # 161
+dsp.runDispatcher(liveForever=1)
diff --git a/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py b/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py
new file mode 100644
index 00000000..bb75c8ef
--- /dev/null
+++ b/examples/v1arch/asyncore/manager/cmdgen/getnext-pull-whole-mib.py
@@ -0,0 +1,71 @@
+"""Command Generator Application (GETNEXT)"""
+from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
+from pysnmp.carrier.asynsock.dgram.udp import UdpSocketTransport
+from pysnmp.proto.api import alpha
+from time import time
+
+# Protocol version to use
+ver = alpha.protoVersions[alpha.protoVersionId1]
+
+# SNMP table header
+headVars = [ ver.ObjectName((1,3,6)) ]
+
+# Create request & response message objects
+req = ver.Message(); rsp = ver.Message()
+req.apiAlphaSetCommunity('public')
+
+# Create PDU, load var-binds, attach PDU to SNMP message
+req.apiAlphaSetPdu(ver.GetNextRequestPdu())
+apply(req.apiAlphaGetPdu().apiAlphaSetVarBindList,
+ map(lambda x, ver=ver: (x.get(), ver.Null()), headVars))
+
+def cbTimerFun(timeNow, startedAt=time()):
+ if timeNow - startedAt > 3:
+ raise "Request timed out"
+
+def cbRecvFun(tspDsp, transportDomain, transportAddress, wholeMsg,
+ req=req, headVars=headVars):
+ rsp = ver.Message()
+ while wholeMsg:
+ wholeMsg = rsp.berDecode(wholeMsg)
+ if req.apiAlphaMatch(rsp):
+ # Check for SNMP errors reported
+ errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus()
+ if errorStatus and errorStatus != 2:
+ raise errorStatus
+
+ # Build SNMP table from response
+ tableIndices = apply(req.apiAlphaGetPdu().apiAlphaGetTableIndices,
+ [rsp.apiAlphaGetPdu()] + headVars)
+
+ # Report SNMP table
+ varBindList = rsp.apiAlphaGetPdu().apiAlphaGetVarBindList()
+ for rowIndices in tableIndices:
+ for cellIdx in filter(lambda x: x!=-1, rowIndices):
+ print transportAddress, \
+ varBindList[cellIdx].apiAlphaGetOidVal()
+
+ # Remove completed SNMP table columns
+ map(lambda idx, headVars=headVars: headVars.__delitem__(idx), \
+ filter(lambda x: x==-1, tableIndices[-1]))
+ if not headVars:
+ raise "EOM"
+
+ # Generate request for next row
+ lastRow = map(lambda cellIdx, varBindList=varBindList:
+ varBindList[cellIdx].apiAlphaGetOidVal(),
+ filter(lambda x: x!=-1, tableIndices[-1]))
+ apply(req.apiAlphaGetPdu().apiAlphaSetVarBindList,
+ map(lambda (x, y): (x.get(), None), lastRow))
+
+ req.apiAlphaGetPdu().apiAlphaGetRequestId().inc(1)
+ tspDsp.sendMessage(
+ req.berEncode(), transportDomain, transportAddress
+ )
+ return wholeMsg
+
+dsp = AsynsockDispatcher(udp=UdpSocketTransport().openClientMode())
+dsp.registerRecvCbFun(cbRecvFun)
+dsp.registerTimerCbFun(cbTimerFun)
+dsp.sendMessage(req.berEncode(), 'udp', ('localhost', 1161)) # 161
+dsp.runDispatcher(liveForever=1)
diff --git a/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py b/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py
new file mode 100644
index 00000000..879f5be3
--- /dev/null
+++ b/examples/v1arch/asyncore/manager/cmdgen/v2c-set.py
@@ -0,0 +1,46 @@
+"""Command Generator Application (GET)"""
+from time import time
+from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
+from pysnmp.carrier.asynsock.dgram.udp import UdpSocketTransport
+from pysnmp.proto.api import alpha
+
+# Protocol version to use
+ver = alpha.protoVersions[alpha.protoVersionId1]
+
+# Build message
+req = ver.Message()
+req.apiAlphaSetCommunity('public')
+
+# Build PDU
+req.apiAlphaSetPdu(ver.SetRequestPdu())
+req.apiAlphaGetPdu().apiAlphaSetVarBindList(
+ # A list of Var-Binds to SET
+ ((1,3,6,1,2,1,1,1,0), ver.Integer(123456)),
+ ((1,3,6,1,2,1,1,1,0), ver.IpAddress('127.0.0.1'))
+ )
+
+def cbTimerFun(timeNow, startedAt=time()):
+ if timeNow - startedAt > 3:
+ raise "Request timed out"
+
+def cbRecvFun(tspDsp, transportDomain, transportAddress, wholeMsg, req=req):
+ rsp = ver.Message()
+ while wholeMsg:
+ wholeMsg = rsp.berDecode(wholeMsg)
+
+ # Make sure this is a response to this request
+ if req.apiAlphaMatch(rsp):
+ errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus()
+ if errorStatus:
+ print 'Error: ', errorStatus
+ else:
+ for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList():
+ print varBind.apiAlphaGetOidVal()
+ tspDsp.doDispatchFlag = 0
+ return wholeMsg
+
+dsp = AsynsockDispatcher(udp=UdpSocketTransport().openClientMode())
+dsp.registerRecvCbFun(cbRecvFun)
+dsp.registerTimerCbFun(cbTimerFun)
+dsp.sendMessage(req.berEncode(), 'udp', ('localhost', 1161)) # 161
+dsp.runDispatcher(liveForever=1)