Caricamento contenuto pagina

È possibile caricare il contenuto per la pagina indicata sotto. Se viene cambiato il nome della pagina, è possibile caricare il contenuto per un'altra pagina. Se il nome della pagina è lasciato vuoto, il nome della pagina viene ipotizzato dal nome del file.

File da cui caricare il contenuto della pagina
Nome pagina
Commento
Tanto va la gatta al lardo che ci lascia lo?

Italiano English
Modifica History Actions

olsrtopologylogger.py

   1 # Copyright 2006 clauz at ninux dot org
   2 # released under the GNU Public License
   3 # 
   4 # OLSR Topology Logger. Dumps dot files from the OLSR dot plugin (http://www.olsr.org).
   5 # Usage: olsrtopologylogger.py [-t timeoutseconds] [-s sleepseconds] [{+,-}h]
   6 #                 [-x n] hostname [port]
   7 # 
   8 #         -t timeoutseconds       set the read timeout to timeoutseconds (default 120).
   9 #         -s sleepseconds         pause sleepseconds between reads (default 300).
  10 #         +h                      use human-readable date and time for filenames.
  11 #         -h                      use floating point seconds since the epoch for filenames (default).
  12 #         -x n            read and dump to file n times (-1=infinite) (default -1)
  13 #         hostname                the hostname
  14 #         port                    the port (default 2004)
  15 # 
  16 # example: olsrtopologylogger.py -x 3 -s 3 localhost
  17 
  18 import telnetlib
  19 import time
  20 import sys
  21 
  22 params={
  23 'PORT':2004, #tcp
  24 'HOST':'127.0.0.1',
  25 'TIMEOUT':120,
  26 'SLEEP':300,
  27 'HUMANREADABLE':False,
  28 'TIMES':-1,
  29 'EXT':'.dot'
  30 }
  31 
  32 def readfromdotplugin(host='127.0.0.1',port='2004',timeout=120, exitonerror=True):
  33 	try:
  34 		dotcon=telnetlib.Telnet(host,port)
  35 	except:
  36 		sys.stderr.write("Error. Can't connect to %s:%s.\n" % (host,port))
  37 		if exitonerror:
  38 			sys.exit(2)
  39 		else:
  40 			return ""
  41 	dotoutput=""
  42 	dotoutput=dotcon.read_until('}',timeout)
  43 	dotoutput+='\n'
  44 	dotcon.close()
  45 	return dotoutput
  46 #readfromdotplugin
  47 	
  48 def gettimestamp(humanreadable=False):
  49 	if humanreadable:
  50 		ts=time.asctime()
  51 		ts=ts.replace(' ','_')
  52 		ts=ts.replace(':','.')
  53 	else:
  54 		ts=time.time()	#seconds since the epoch
  55 	return ts
  56 #gettimestamp
  57 
  58 def processoptions(argvlist,params):
  59 	if len(argvlist)<2:
  60 		instructions= "OLSR Topology Logger. Dumps dot files from the OLSR dot plugin (http://www.olsr.org).\n"
  61 		instructions+="Usage: %s [-t timeoutseconds] [-s sleepseconds] [{+,-}h] \n" % (argvlist[0])
  62 		instructions+="\t\t[-x n] hostname [port]\n\n"
  63 		instructions+="\t-t timeoutseconds\tset the read timeout to timeoutseconds (default %d).\n" % (params['TIMEOUT'],)
  64 		instructions+="\t-s sleepseconds  \tpause sleepseconds between reads (default %d).\n" % (params['SLEEP'],)
  65 		instructions+="\t+h               \tuse human-readable date and time for filenames"
  66 		if params['HUMANREADABLE']:
  67 			instructions+=" (default).\n"
  68 		else:
  69 			instructions+=".\n"
  70 		instructions+="\t-h               \tuse floating point seconds since the epoch for filenames"
  71 		if not params['HUMANREADABLE']:
  72 			instructions+=" (default).\n"
  73 		else:
  74 			instructions+=".\n"
  75 		instructions+="\t-x n           \tread and dump to file n times (-1=infinite) (default %s)\n" % (params['TIMES'],)
  76 		instructions+="\thostname         \tthe hostname\n"
  77 		instructions+="\tport             \tthe port (default %s)\n" % (params['PORT'],)
  78 # 		print instructions
  79 		sys.stderr.write(instructions)
  80 		sys.exit(1)
  81 	else:
  82 		i=1
  83 		while i<len(argvlist):
  84 			arg=argvlist[i]
  85 			if arg=='+h':
  86 				params['HUMANREADABLE']=True
  87 			elif arg=='-h':
  88 				params['HUMANREADABLE']=False
  89 			elif arg=='-t':
  90 				try:
  91 					timeoutseconds=argvlist[i+1]
  92 					params['TIMEOUT']=int(timeoutseconds)
  93 					i+=1
  94 				except:
  95 					pass
  96 			elif arg=='-s':
  97 				try:
  98 					sleepseconds=argvlist[i+1]
  99 					params['SLEEP']=int(sleepseconds)
 100 					i+=1
 101 				except:
 102 					pass
 103  			elif arg=='-x':
 104 				try:
 105 					n=argvlist[i+1]
 106 					params['TIMES']=int(n)
 107 					i+=1
 108 				except:
 109 					pass
 110 			else:
 111 				params['HOST']=arg
 112 				try:
 113 					port=argvlist[i+1]
 114 					params['PORT']=int(port)
 115 					i+=1
 116 				except:
 117 					pass
 118 			i+=1
 119 #processoptions		
 120 
 121 
 122 if __name__=="__main__":
 123 	
 124 	processoptions(sys.argv,params)
 125 	
 126 	try:
 127 		firstiteration=True
 128 		iterations=params['TIMES']	
 129 		while iterations==-1 or iterations>0:
 130 			print "reading..."
 131 			dotoutput=readfromdotplugin(params['HOST'],params['PORT'],params['TIMEOUT'],firstiteration)
 132 			firstiteration=False
 133 			timestamp=gettimestamp(params['HUMANREADABLE'])
 134 			
 135 			filename='%s' % (timestamp,)
 136 			filename+=params['EXT']
 137 			
 138 			try:
 139 				try:
 140 					dotfile=file(filename,'r')
 141 					dotfile.close()
 142 					erstr="Warning! File %s already exists! Skipping...",(filename,)
 143 # 					sys.stderr.write(erstr)
 144 					print erstr
 145 				except IOError:
 146 					dotfile=file(filename,'w')
 147 					dotfile.write(dotoutput)
 148 					dotfile.close()
 149 					print "%s: %s created." % (time.asctime(),filename)
 150 			except:
 151 				sys.stderr.write("I/O Error!!")
 152 				sys.exit(2)
 153 			
 154 			
 155 			print "sleeping %d seconds..." % (params['SLEEP'],)
 156 			time.sleep(params['SLEEP'])
 157 			
 158 			if iterations>0: 
 159 				iterations-=1
 160 		#while
 161 	except KeyboardInterrupt:
 162 		pass