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 oidSet = [] 9 while oids: 10 set = tuple(oids[:2]) 11 if len(set) == 2: 12 oidSet.append( set ) 13 oids = oids[2:] 14 df = proxy.set( 15 oidSet, timeout=.25, retryCount=5 16 ) 17 df.addCallback( printResults ) 18 df.addCallback( exiter ) 19 df.addErrback( errorReporter ) 20 df.addErrback( exiter ) 21 return df 22 23 def printResults( result ): 24 print 'Results:' 25 pprint.pprint( result ) 26 return result 27 28 def errorReporter( err ): 29 print 'ERROR', err.getTraceback() 30 return err 31 def exiter( value ): 32 reactor.stop() 33 return value 34 35 36 if __name__ == "__main__": 37 import sys, logging 38 logging.basicConfig() 39 # need to get the ip address 40 usage = """Usage: 41 simpleget ipAddress community baseoid... 42 43 ipAddress -- dotted IP address of the agent 44 community -- community string for the agent 45 baseoid -- dotted set of OIDs to retrieve from agent 46 """ 47 if len(sys.argv) < 3: 48 print usage 49 sys.exit( 1 ) 50 ipAddress = sys.argv[1] 51 # choose random port in range 25000 to 30000 52 port = snmpprotocol.port() 53 proxy = agentproxy.AgentProxy( 54 ipAddress, 161, 55 community = sys.argv[2], 56 snmpVersion = 'v2', 57 protocol = port.protocol, 58 ) 59 if not sys.argv[3:]: 60 oids = [ 61 '.1.3.6.1.2.1.1.1.0', 'New Description', 62 '.1.3.6.1.2.1.1.3.0', '11111111', 63 ] 64 else: 65 oids = sys.argv[3:] 66 reactor.callWhenRunning( main, proxy, oids ) 67 reactor.run()