diff options
Diffstat (limited to 'examples/v1arch/manager/nextgen.py')
-rw-r--r-- | examples/v1arch/manager/nextgen.py | 75 |
1 files changed, 31 insertions, 44 deletions
diff --git a/examples/v1arch/manager/nextgen.py b/examples/v1arch/manager/nextgen.py index 20be7ca..846ee40 100644 --- a/examples/v1arch/manager/nextgen.py +++ b/examples/v1arch/manager/nextgen.py @@ -1,6 +1,6 @@ """Command Generator Application (GETNEXT)""" from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher -from pysnmp.carrier.asynsock.dgram.udp import UdpSocketTransport +from pysnmp.carrier.asynsock.dgram import udp from pyasn1.codec.ber import encoder, decoder from pysnmp.proto import api from time import time @@ -30,11 +30,10 @@ def cbTimerFun(timeNow): if timeNow - startedAt > 3: raise "Request timed out" -def cbRecvFun(tspDsp, transportDomain, transportAddress, wholeMsg, - reqPDU=reqPDU, headVars=headVars): +def cbRecvFun(transportDispatcher, transportDomain, transportAddress, + wholeMsg, reqPDU=reqPDU, headVars=headVars): while wholeMsg: rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=pMod.Message()) -# print rspMsg.prettyPrinter() rspPDU = pMod.apiMessage.getPDU(rspMsg) # Match response to request if pMod.apiPDU.getRequestID(reqPDU)==pMod.apiPDU.getRequestID(rspPDU): @@ -42,33 +41,27 @@ def cbRecvFun(tspDsp, transportDomain, transportAddress, wholeMsg, errorStatus = pMod.apiPDU.getErrorStatus(rspPDU) if errorStatus and errorStatus != 2: raise errorStatus - # Build SNMP table from response - tableIndices = pMod.apiPDU.getTableIndices( - reqPDU, rspPDU, headVars - ) + # Format var-binds table + varBindTable = pMod.apiPDU.getVarBindTable(reqPDU, rspPDU) # Report SNMP table - varBindList = pMod.apiPDU.getVarBindList(rspPDU) - for rowIndices in tableIndices: - for cellIdx in filter(lambda x: x!=-1, rowIndices): - print transportAddress, - print pMod.apiVarBind.getOIDVal(varBindList[cellIdx]) - - # 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" - + for tableRow in varBindTable: + for name, val in tableRow: + print 'from: %s, %s = %s' % ( + transportAddress, name, val + ) + # Stop on EOM + for oid, val in varBindTable[-1]: + if val is not None: + break + else: + transportDispatcher.stopDispatcher() + # Generate request for next row - lastRow = [] - for cellIdx in filter(lambda x: x!=-1, tableIndices[-1]): - lastRow.append( - (pMod.apiVarBind.getOIDVal(varBindList[cellIdx])[0], - pMod.Null()) - ) - pMod.apiPDU.setVarBinds(reqPDU, lastRow) + pMod.apiPDU.setVarBinds( + reqPDU, map(lambda (x,y),n=pMod.Null(): (x,n), varBindTable[-1]) + ) pMod.apiPDU.setRequestID(reqPDU, pMod.getNextRequestID()) - tspDsp.sendMessage( + transportDispatcher.sendMessage( encoder.encode(reqMsg), transportDomain, transportAddress ) global startedAt @@ -77,19 +70,13 @@ def cbRecvFun(tspDsp, transportDomain, transportAddress, wholeMsg, startedAt = time() return wholeMsg -dsp = AsynsockDispatcher(udp=UdpSocketTransport().openClientMode()) -dsp.registerRecvCbFun(cbRecvFun) -dsp.registerTimerCbFun(cbTimerFun) -#dsp.sendMessage(req.berEncode(), 'udp', ('localhost', 1161)) # 161 -dsp.sendMessage(encoder.encode(reqMsg), 'udp', ('ts29.moscow.net.rol.ru', 161)) - -try: - msgAndPduDsp.transportDispatcher.runDispatcher() -except "EOM": - pass - -#def f(): -# dsp.runDispatcher(liveForever=1) -#f() -#import profile -#profile.run('f()') +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.runDispatcher() |