P:\twistedsnmp\doc\examples\simpleget.py

        1 """Trivial example to retrieve an OID from a remote Agent"""
        2 from twisted.internet import reactor
        3 from twistedsnmp import snmpprotocol, agentproxy
        4 import pprint
        5
        6 def main( proxy, oids ):
        7     """Do a getTable on proxy for OIDs and store in oidStore"""
        8     df = proxy.get(
        9         oids, timeout=.25, retryCount=5
       10     )
       11     df.addCallback( printResults )
       12     df.addCallback( exiter )
       13     df.addErrback( errorReporter )
       14     df.addErrback( exiter )
       15     return df
       16
       17 def printResults( result ):
       18     print 'Results:'
       19     pprint.pprint( result )
       20     return result
       21
       22 def errorReporter( err ):
       23     print 'ERROR', err.getTraceback()
       24     return err
       25 def exiter( value ):
       26     reactor.stop()
       27     return value
       28
       29
       30 if __name__ == "__main__":
       31     import sys, logging
       32     logging.basicConfig()
       33     # need to get the ip address
       34     usage = """Usage:
       35     simpleget ipAddress community baseoid...
       36
       37 ipAddress -- dotted IP address of the agent
       38 community -- community string for the agent
       39 baseoid -- dotted set of OIDs to retrieve from agent
       40 """
       41     if len(sys.argv) < 3:
       42         print usage
       43         sys.exit( 1 )
       44     ipAddress = sys.argv[1]
       45     # choose random port in range 25000 to 30000
       46     port = snmpprotocol.port()
       47     proxy = agentproxy.AgentProxy(
       48         ipAddress, 161,
       49         community = sys.argv[2],
       50         snmpVersion = 'v2',
       51         protocol = port.protocol,
       52     )
       53     if not sys.argv[3:]:
       54         oids = [
       55             '.1.3.6.1.2.1.1.1.0', # sysDescr
       56             '.1.3.6.1.2.1.1.3.0', # uptime
       57         ]
       58     else:
       59         oids = sys.argv[3:]
       60     reactor.callWhenRunning( main, proxy, oids )
       61     reactor.run()