# parse the output of static.ml and generate static.sh's for each node. # lvoge@cs.vu.nl # # Copyright 2004 Lodewijk Voge / Stichting Wireless Leiden, # All Rights Reserved. # License: http://www.wirelessleiden.nl/LICENSE. import os import re import sys import popen2 nodere = re.compile("^(.*):") routere = re.compile("^\t([^ ]*) -> ([^, ]*)") routedict = {} node = None for l in open(sys.argv[1]).readlines(): l = l[:-1] match = nodere.search(l) if match != None: node = match.group(1) routedict[node] = [] match = routere.search(l) if match != None: routedict[node].append( (match.group(1), match.group(2) ) ) def snarf_output(cmd): (cin, cout) = popen2.popen2(cmd) cout.close() return cin.read()[:-1] hostname = snarf_output("hostname") whoami = snarf_output("whoami") date = snarf_output("date") svnrev = open("svnrev").read()[:-1] static = """#!/bin/sh # # Created by static.ml, a static routing generator for Wireless Leiden # # SVN Genesis Revision: %s # Node: %%s # # %s at %s by %s # # add="route -q add" loops="" route -q flush 2> /dev/null while sleep 1 do netstat -nr | grep -q UGSc || break loops="x\$loops" echo Waiting for route table to flush: $loops if [ "\$loops" = "xxxxxxxxxx" ] then break fi route -q flush 2> /dev/null done sysctl net.inet.ip.forwarding=1 """ % (svnrev, date, hostname, whoami) try: os.mkdir("output") except: pass for node in routedict: try: os.mkdir("output/%s" % node) except: pass adds = [] for r in routedict[node]: adds.append("route add %s %s" % r) staticsh = (static % node) + "\n".join(adds) open("output/%s/static.sh" % node, "w").write(staticsh)