#!/usr/local/bin/python # # Program to walk through the wleiden network # # 22-08-04 verbose + debug added # fixed bug when using if on node_list[0] # 21-08-04 first working code # 20-08-04 init import re import string import sys import math global node_list, node_done, ip_mask global verbose, debug node_list = [] node_done = [] ip_mask = [] verbose = 0 debug = 0 def create_nodetable(): for l in file.readlines(): l = l.split('|') li = [] for n in l[1:]: n = n.split('/') n.pop() li.append(n) ip_mask.append(li) node_list.append(l[0]) def known_name(nodename): for l, n in enumerate(node_list): if n == nodename: return l return -1 def ip2node(ip): p = ip.split('.') # check if ip address is valid if len(p) == 4: for l, m in enumerate(ip_mask): for n in m: if ip == n[0]: return l return -1 def walk_node_done(nodename): for n in node_done: if n == nodename: return 0 return 1 def list_rmil_node(nodename): rm_il = [] #ReMoteInterlink l = known_name(nodename) for m in ip_mask[l]: ip = m[0].split('.') if ip[1] == '16': # Get All the other ipaddresses in subnet; works only with cidr >= 24 size = pow(2,32-int(m[1])) # 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 def walk(ip): l = ip2node(ip) if verbose: print ip + '\t#=>\t' + node_list[l] else: print ip node_done.append(node_list[l]) for p in node_done: for m in list_rmil_node(p): l = ip2node(m) if l != -1: if walk_node_done(node_list[l]): if verbose: print m + '\t#=>\t' + node_list[l] else: print m node_done.append(node_list[l]) if debug: print node_done print 'nodes done: ' + str(len(node_done)) print node_list print 'nodes list: ' + str(len(node_list)) def usage(): print 'usage: ' + sys.argv[0] + ' [ -h | starting interlink ip ] [ -d | -v ]' print '-h for extra information' print '-d for debug output' print '-v for verbose output' def show_help(): print '''This program walks through the WirelessLeiden network. It is used to update the static routing of the network. It shows the interlink ips by starting at the given node and listing nodes that are one step further. This process is repeated until all the nodes are reached. Written by Roland van Laar Thanks to Rick van der Zwet and Lodewijk Voge''' usage() file = open('nodes') create_nodetable() if len(sys.argv) == 3: if sys.argv[2] == '-v': verbose = 1 elif sys.argv[2] == '-d': debug = 1 verbose = 1 if len(sys.argv) >= 2: if sys.argv[1] == '-h': show_help() elif ip2node(sys.argv[1]) != -1: walk(sys.argv[1]) else: print 'Unknown option or ipaddress: ' + sys.argv[1] else: usage()