#!/usr/local/bin/python # #Get NodeName of ipdress # #Wireless Leiden spider tool. #0.14: 23/8/2004 copied a funtion from Roland van Laar to add functionality of multiple netmasks # add some extra visual fixes #0.13: 22/8/2004 add options funtions # wrote help function # wrote some extra help info into the program to make debugging easy #0.12: 17/8/2004 fixed crash when odd numbers of nodes are present # fixed crash when trying to jump to other node # both fixed thanx to lodewijk #0.11: 16/8/2004 add option to select nodes from a table #0.10: 16/8/2004 first commit #0.01: 15/8/2004 first create import re import string import sys import math #version number version = '0.14' #file to open file = open('nodes') #table which has all the information , about the nodes global node_table node_table = [] #create nodelist, main table, all data is get out of file nodes global node_list, node_done, ip_mask global verbose, debug def create_nodelist(): for l in file.readlines(): l = l.split('|') ip_numbers = [] netmask = [] for n in l[1:]: n = n.split('/') ip_numbers.append(n[0]) #I only need ip numbers netmask.append(n[1]) node_table.append([l[0],ip_numbers,netmask]) #format[hostname,ipadresses,netmask] #check if ip-adress is valid #INPUT: string, ipadress to check #OUTPUT: 1 = ipaddress is valid, 0 = ipaddress is not valid def ipchecker(string2check): ip2check = string2check.split('.') if len(ip2check) != 4: return 0 else: for l in range(len(ip2check)): if re.search('\D' ,ip2check[l]): return 0 return 1 #check if nodename exist #INPUT: string, nodenode to check #OUTPUT: if nodename exist,return line number, else 999 if not found def checkvalidnodename(cnodename): for n in range(len(node_table)): if node_table[n][0] == cnodename: return n return 999 #check to which node a ip adress belongs #INPUT string, ipadress #OUTPUT string, nodename, None is not exist def getnodename(ipaddress): for n in range(len(node_table)): for r in range(len(node_table[n][1])): if node_table[n][1][r] == ipaddress: return node_table[n][0] #give statictics from a node #INPUT string, nodename #OUTPUT List, string of interlink adresses def getnodeinfo(cnodename): print 'statics for node: ' + cnodename print 'ip addresses' for l in range(len(node_table)): if node_table[l][0] == cnodename: il_rm_list = [] for n,r in enumerate(node_table[l][1]): ip = r il_s = ip.split('.') if il_s[1] == '16': ip_range = list_allip(r,node_table[l][2][n]) for ip_address in enumerate(ip_range): for line in range(len(node_table)): for pos_ip in enumerate(node_table[line][1]): if ip_address[1] == pos_ip[1]: il_rm_list.append(ip_address[1]) print str(ip) + '\t=>\t' + str(ip_address[1]) + '\tof\t' \ + tabfix(str(getnodename(ip_address[1]))) + 'number : ' + \ str(len(il_rm_list)-1) elif il_s[1] == '31': print str(ip) + '\t\t\t\tof\thccproject' else: print str(ip) + '\t\t\t\tof\t' + str(getnodename(ip)) return il_rm_list #get all ipadresses in selected range #INPUT string, ipadress # string, subnet #OUTPUT list, all ipadresses def list_allip(ipaddress,subnet): rm_il = [] #ReMoteInterlink ip = ipaddress.split('.') # Get All the other ipaddresses in subnet; works only with cidr >= 24 size = pow(2,32-int(subnet)) # get the subnet size nr = int(ip[3]) for n in range(256/size): if n*size+1 <= nr and nr <= (n+1)*size-2: # + 1 and -2 to rmove network and broadcast address for q in range(n*size+1,(n+1)*size-1): if q != nr: rm_ip = string.join([ip[0],ip[1],ip[2],str(q)],'.') rm_il.append(rm_ip) return rm_il #main program to avoid loops #INPUT string, first ipadress #OUTPUT none def run(ipaddress): number = 0 if ipchecker(ipaddress): while number != 99: if getnodename(ipaddress): ip_list = getnodeinfo(getnodename(ipaddress)) else: print 'Sorry, i will not be able to find information for ' + str(ipaddress) print '99 to quit' print '90 to select a node' number = int(raw_input('Enter number: ')) if number == 90: show_nodelist() elif number == 99: sys.exit(0) elif number < len(ip_list): ipaddress = ip_list[number] else: print '\n\n' + str(number) + ' seems to be a WRONG INPUT please enter a valid number\n\n' break else: print str(ipaddress) + 'ain\'t a valid ipadress' #create beautifull output #INPUT string, node #OUTPUT string, node + added with tabs def tabfix(node): rt = '' length = len(node) if length < 8: rt = node + '\t\t\t' elif length < 16: rt = node + '\t\t' else: rt = node + '\t' return rt #show nodelist so user can select a input #INPUT none #OUTPUT none def show_nodelist(): tmp = len(node_table) while tmp >= len(node_table): n = 0 while n < len(node_table): print tabfix(node_table[n][0]) + 'number : ' + str(n), if n < len(node_table) - 1: if len(node_table[n+1][0]) > 5: print '\t' + tabfix(node_table[n + 1][0]) + 'number : ' + str(n+1) else: print '\t' + tabfix(node_table[n + 1][0]) + 'number : ' + str(n+1) else: print '' n = n + 2 print '999 to quit' tmp = int(raw_input('Enter number: ')) if tmp < len(node_table): run(node_table[tmp][1][0]) elif tmp == 999: sys.exit(0) else: print str(tmp) + ' WRONG INPUT!, try again' #print list of online nodes #INPUT none #OUTPUT none def make_online_list(): nodelist = "" for n in range(len(node_table)): nodelist = nodelist + ' ' + node_table[n][0] print nodelist.lstrip(' ') #print helpfile #INPUT none #OUTPUT none def helpfile(): print '\nWirelessLeiden Configuration Checker version ' + version print '\nBy using this program, you can get information about the WirelessLeiden node configuration' print 'You can only use 1 option at the time' print 'Options \n' print '-c CNODENAME \t\t Check if a nodename is valid' print '-h \t\t\t This helpfile' print '-i IPADDRESS/CNODENAME \t Get info about this IPADDRES/CNODENAME' print '-n IPADDRESS \t\t Check which node own this IPADDRESS' print 'online_list\t\t Make List of nodes who are marked as \'online\'' print '-v \t\t\t Show version number' print 'no options \t\t Get list al node and will be able to select a node' print '\n\nSpecial thanks to:' print 'Lodewijk Voge' print 'Roland van Laar' print 'and all other WirelessLeiden volunteers' #Input line checker #INPUT none #OUTPUT none #create nodelist create_nodelist() if len(sys.argv) == 2: if sys.argv[1] == 'online_list': make_online_list() elif sys.argv[1] == '-h': helpfile() elif sys.argv[1] == '-v': print version elif ipchecker(sys.argv[1]): run(sys.argv[1]) else: tmp = checkvalidnodename(sys.argv[1]) if tmp != 999: run(node_table[tmp][1][0]) else: print sys.argv[1] + ' ain\'t a valid nodename' show_nodelist() elif len(sys.argv) == 3: if sys.argv[1] == '-h': helpfile() elif sys.argv[1] == '-n': print getnodename(sys.argv[2]) elif sys.argv[1] == '-c': if checkvalidnodename(sys.argv[2]) == 999: print '0' else: print '1' elif sys.argv[1] == '-i': if ipchecker(sys.argv[2]): getnodeinfo(getnodename(sys.argv[2])) elif checkvalidnodename(sys.argv[2]) != 999: getnodeinfo(sys.argv[2]) else: print sys.argv[2] + ' ain\'t a valid IP address or CNODENAME' else: show_nodelist()