2 # vim: set et sw=4 sts=4 :
8 for c in line.split('\t'):
9 r.append(c.strip('"\n'))
16 if line.startswith('"tabla de ruteo de '):
20 hosts[host]['ifaces'] = []
21 while line and line.strip() <> '"red"\t"ip"\t"mascara"\t"gateway"\t"interfaz"\t"metrica"':
22 if line.startswith('"'):
23 (iface, ip, mascara, broadcast, peer) = get_campos(line)[:5]
24 hosts[host]['ifaces'].append({
28 'broadcast': broadcast,
33 hosts[host]['rutas'] = []
34 while line and line.strip() <> '"--"':
35 if line.startswith('"'):
36 (red, ip, mascara, gateway, iface, metrica) = get_campos(line)
37 if not metrica: metrica = "1"
38 hosts[host]['rutas'].append({
51 os.system('cp /etc/hosts /etc/hosts.bak')
52 os.system('cp hosts /etc/hosts')
53 os.system('cp /etc/networks /etc/networks.bak')
54 os.system('cp networks /etc/networks')
57 for iface in hosts[host]['ifaces']:
58 if iface['iface'].startswith('ppp'):
59 if host == 'h131' or host == 'h118':
60 print 'UP: pppd -detach modem crtscts lock %(ip)s:%(peer)s /dev/ttyS0 9600' % iface
61 os.system('pppd -detach modem crtscts lock %(ip)s:%(peer)s /dev/ttyS0 9600 &' % iface)
63 print 'UP: pppd -detach crtscts lock %(ip)s:%(peer)s /dev/ttyS0' % iface
64 os.system('pppd -detach crtscts lock %(ip)s:%(peer)s /dev/ttyS0 &' % iface)
67 print 'UP: ifconfig %(iface)s %(ip)s broadcast %(broadcast)s netmask %(mascara)s' % iface
68 os.system('ifconfig %(iface)s %(ip)s broadcast %(broadcast)s netmask %(mascara)s' % iface)
70 print 'while sleep 1; do if ping -c1 %(peer)s 2>&1 > /dev/null; then break; else echo Esperando link ppp...; fi; done' % iface
71 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)
72 for ruta in hosts[host]['rutas']:
73 if ruta['metrica'] <> "0":
74 print 'UP: route add -net %(ip)s gw %(gateway)s netmask %(mascara)s dev %(iface)s metric %(metrica)s' % ruta
75 os.system('route add -net %(ip)s gw %(gateway)s netmask %(mascara)s dev %(iface)s metric %(metrica)s' % ruta)
79 for iface in hosts[host]['ifaces']:
80 print 'DOWN: ifconfig %(iface)s down' % iface
81 if iface['iface'].startswith('ppp'):
82 os.system('killall pppd')
84 os.system('ifconfig %(iface)s down' % iface)
85 os.system('mv /etc/hosts.bak /etc/hosts')
86 os.system('mv /etc/networks.bak /etc/networks')
90 for host in hosts.keys():
93 if __name__ == '__main__':
95 print >>sys.stderr, 'Uso: %s [up|down] host' % sys.argv[0]
97 fd = file('rutas.csv')
98 if sys.argv[1] == 'up':
100 elif sys.argv[1] == 'down':
101 down(sys.argv[2], fd)
102 elif sys.argv[1] == 'restart':
103 down(sys.argv[2], fd)
104 up(sys.argv[2], file('rutas.csv'))
105 elif sys.argv[1] == 'list':