X-Git-Url: https://git.llucax.com/z.facultad/75.43/tp2.git/blobdiff_plain/549744ae7e50106cc84606c7b7fb1750c98a4ceb..5a6992b097deab225a043fad61bb84d11df28ded:/makeroute diff --git a/makeroute b/makeroute index 09072bc..fd7b223 100755 --- a/makeroute +++ b/makeroute @@ -1,6 +1,8 @@ #!/usr/bin/env python # vim: set et sw=4 sts=4 : +import sys, os + def get_campos(line): r = [] for c in line.split('\t'): @@ -18,12 +20,13 @@ def parse(f): hosts[host]['ifaces'] = [] while line and line.strip() <> '"red"\t"ip"\t"mascara"\t"gateway"\t"interfaz"\t"metrica"': if line.startswith('"'): - (iface, ip, mascara, broadcast) = get_campos(line)[:4] + (iface, ip, mascara, broadcast, peer) = get_campos(line)[:5] hosts[host]['ifaces'].append({ 'iface': iface, 'ip': ip, 'mascara': mascara, 'broadcast': broadcast, + 'peer': peer, }) line = f.readline() line = f.readline() @@ -44,19 +47,57 @@ def parse(f): line = f.readline() return hosts -def main(host): - import sys, pprint - #pprint.pprint(parse(sys.stdin)) - #sys.exit(1) - hosts = parse(sys.stdin) +def up(host, fd): + hosts = parse(fd) + ppp = 0 for iface in hosts[host]['ifaces']: - if not iface['iface'].startswith('ppp'): - print 'ifconfig %(iface)s %(ip)s broadcast %(broadcast)s netmask %(mascara)s' % iface + if iface['iface'].startswith('ppp'): + print 'UP: pppd -detach crtscts lock %(ip)s:%(peer)s /dev/ttyS0' % iface + os.system('pppd -detach crtscts lock %(ip)s:%(peer)s /dev/ttyS0 &' % iface) + ppp = 1 + else: + print 'UP: ifconfig %(iface)s %(ip)s broadcast %(broadcast)s netmask %(mascara)s' % iface + os.system('ifconfig %(iface)s %(ip)s broadcast %(broadcast)s netmask %(mascara)s' % iface) + if ppp: + print 'while sleep 1; do if ping -c1 %(peer)s 2>&1 > /dev/null; then break; else echo Esperando link ppp...; fi; done' % iface + os.system('while sleep 1; do if ping -c1 %(peer)s 2>&1 > /dev/null; then break; else echo Esperando link ppp...; fi; done' % iface) for ruta in hosts[host]['rutas']: if ruta['metrica'] <> "0": - print 'route add -net %(ip)s gw %(gateway)s netmask %(mascara)s dev %(iface)s metric %(metrica)s' % ruta + print 'UP: route add -net %(ip)s gw %(gateway)s netmask %(mascara)s dev %(iface)s metric %(metrica)s' % ruta + os.system('route add -net %(ip)s gw %(gateway)s netmask %(mascara)s dev %(iface)s metric %(metrica)s' % ruta) + os.system('cp /etc/hosts /etc/hosts.13.bak') + os.system('cp hosts /etc/hosts') + os.system('cp /etc/networks /etc/networks.13.bak') + os.system('cp networks /etc/networks') + +def down(host, fd): + hosts = parse(fd) + for iface in hosts[host]['ifaces']: + print 'DOWN: ifconfig %(iface)s down' % iface + if iface['iface'].startswith('ppp'): + os.system('killall pppd') + else: + os.system('ifconfig %(iface)s down' % iface) + os.system('mv /etc/hosts.13.bak /etc/hosts') + os.system('mv /etc/networks.13.bak /etc/networks') + +def list(fd): + hosts = parse(fd) + for host in hosts.keys(): + print host if __name__ == '__main__': - import sys - main(sys.argv[1]) + fd = file('routers_labo.csv') + if len(sys.argv) < 2: + print >>sys.stderr, 'Uso: %s [up|down] host' % sys.argv[0] + sys.exit(1) + if sys.argv[1] == 'up': + up(sys.argv[2], fd) + elif sys.argv[1] == 'down': + down(sys.argv[2], fd) + elif sys.argv[1] == 'restart': + down(sys.argv[2], fd) + up(sys.argv[2], file('routers_labo.csv')) + elif sys.argv[1] == 'list': + list(fd)