diff options
author | elie <elie> | 2004-11-05 19:26:44 +0000 |
---|---|---|
committer | elie <elie> | 2004-11-05 19:26:44 +0000 |
commit | 0902d3cc02b1f790f40c22a527a7c9f46ec85489 (patch) | |
tree | 706c5993ac370b79dec0aeb4ab6eadc1f4a14de6 /examples/v1arch | |
parent | dded1b32e89813bd26a24b5b57c02bad0caac11b (diff) | |
download | pysnmp-0902d3cc02b1f790f40c22a527a7c9f46ec85489.tar.gz |
legacy examples moved under v1arch/
Diffstat (limited to 'examples/v1arch')
-rw-r--r-- | examples/v1arch/manager/getgen.py | 43 | ||||
-rw-r--r-- | examples/v1arch/manager/nextgen.py | 71 | ||||
-rw-r--r-- | examples/v1arch/manager/setgen.py | 46 |
3 files changed, 160 insertions, 0 deletions
diff --git a/examples/v1arch/manager/getgen.py b/examples/v1arch/manager/getgen.py new file mode 100644 index 0000000..f4a518f --- /dev/null +++ b/examples/v1arch/manager/getgen.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/manager/nextgen.py b/examples/v1arch/manager/nextgen.py new file mode 100644 index 0000000..bb75c8e --- /dev/null +++ b/examples/v1arch/manager/nextgen.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/manager/setgen.py b/examples/v1arch/manager/setgen.py new file mode 100644 index 0000000..879f5be --- /dev/null +++ b/examples/v1arch/manager/setgen.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) |