#FORMAT python #! /usr/bin/env python # Copyright 2006 clauz at ninux dot org # released under the GNU Public License # # OLSR Topology Logger. Dumps dot files from the OLSR dot plugin (http://www.olsr.org). # Usage: olsrtopologylogger.py [-t timeoutseconds] [-s sleepseconds] [{+,-}h] # [-x n] hostname [port] # # -t timeoutseconds set the read timeout to timeoutseconds (default 120). # -s sleepseconds pause sleepseconds between reads (default 300). # +h use human-readable date and time for filenames. # -h use floating point seconds since the epoch for filenames (default). # -x n read and dump to file n times (-1=infinite) (default -1) # hostname the hostname # port the port (default 2004) # # example: olsrtopologylogger.py -x 3 -s 3 localhost import telnetlib import time import sys params={ 'PORT':2004, #tcp 'HOST':'127.0.0.1', 'TIMEOUT':120, 'SLEEP':300, 'HUMANREADABLE':False, 'TIMES':-1, 'EXT':'.dot' } def readfromdotplugin(host='127.0.0.1',port='2004',timeout=120, exitonerror=True): try: dotcon=telnetlib.Telnet(host,port) except: sys.stderr.write("Error. Can't connect to %s:%s.\n" % (host,port)) if exitonerror: sys.exit(2) else: return "" dotoutput="" dotoutput=dotcon.read_until('}',timeout) dotoutput+='\n' dotcon.close() return dotoutput #readfromdotplugin def gettimestamp(humanreadable=False): if humanreadable: ts=time.asctime() ts=ts.replace(' ','_') ts=ts.replace(':','.') else: ts=time.time() #seconds since the epoch return ts #gettimestamp def processoptions(argvlist,params): if len(argvlist)<2: instructions= "OLSR Topology Logger. Dumps dot files from the OLSR dot plugin (http://www.olsr.org).\n" instructions+="Usage: %s [-t timeoutseconds] [-s sleepseconds] [{+,-}h] \n" % (argvlist[0]) instructions+="\t\t[-x n] hostname [port]\n\n" instructions+="\t-t timeoutseconds\tset the read timeout to timeoutseconds (default %d).\n" % (params['TIMEOUT'],) instructions+="\t-s sleepseconds \tpause sleepseconds between reads (default %d).\n" % (params['SLEEP'],) instructions+="\t+h \tuse human-readable date and time for filenames" if params['HUMANREADABLE']: instructions+=" (default).\n" else: instructions+=".\n" instructions+="\t-h \tuse floating point seconds since the epoch for filenames" if not params['HUMANREADABLE']: instructions+=" (default).\n" else: instructions+=".\n" instructions+="\t-x n \tread and dump to file n times (-1=infinite) (default %s)\n" % (params['TIMES'],) instructions+="\thostname \tthe hostname\n" instructions+="\tport \tthe port (default %s)\n" % (params['PORT'],) # print instructions sys.stderr.write(instructions) sys.exit(1) else: i=1 while i0: print "reading..." dotoutput=readfromdotplugin(params['HOST'],params['PORT'],params['TIMEOUT'],firstiteration) firstiteration=False timestamp=gettimestamp(params['HUMANREADABLE']) filename='%s' % (timestamp,) filename+=params['EXT'] try: try: dotfile=file(filename,'r') dotfile.close() erstr="Warning! File %s already exists! Skipping...",(filename,) # sys.stderr.write(erstr) print erstr except IOError: dotfile=file(filename,'w') dotfile.write(dotoutput) dotfile.close() print "%s: %s created." % (time.asctime(),filename) except: sys.stderr.write("I/O Error!!") sys.exit(2) print "sleeping %d seconds..." % (params['SLEEP'],) time.sleep(params['SLEEP']) if iterations>0: iterations-=1 #while except KeyboardInterrupt: pass