source: fedkit/quagga_routing.pl @ 17c2f7b

compt_changes
Last change on this file since 17c2f7b was 17c2f7b, checked in by Ted Faber <faber@…>, 12 years ago

Ubuntu wrouting using quagga

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use IO::File;
5use IO::Pipe;
6
7my $IFCONFIG = "/sbin/ifconfig";
8my $ZEBRA= "/usr/lib/quagga/zebra";
9my $OSPFD= "/usr/lib/quagga/ospfd";
10
11my $ospffn = "/usr/local/federation/etc/ospfd.conf";
12my $zebrafn = "/usr/local/federation/etc/zebra.conf";
13my @live_ones;
14my @bits_in_quad;
15my %ainfo;
16
17sub quad_to_int {
18    my($ip) = @_;
19    my @q = split(/\./, $ip);
20    return ($q[0] << 24) + ($q[1] << 16) +  ($q[2] << 8) + $q[3];
21}
22
23sub int_to_quad {
24    my($i) = @_;
25    return sprintf("%d.%d.%d.%d", ($i >> 24) & 0xff, ($i>>16) & 0xff, 
26        ($i >>8) & 0xff, $i & 0xff);
27}
28
29sub nbits {
30    my($b) = @_;
31    return 0 if $b == 0;
32    my $r = 1;
33    for (my $i = 1 ; $i < $b; $i++) {
34        $r <<= 1;
35        $r += 1;
36    }
37    for (my $i = $b;  $i< 32; $i ++) {
38        $r <<= 1;
39        $r &= 0xfffffffe;
40    }
41    return $r;
42}
43
44sub netslash {
45    my($ip, $slash) = @_;
46    return int_to_quad(quad_to_int($ip) & nbits($slash));
47}
48
49for (my $i =0; $i < 256; $i++) {
50    $bits_in_quad[$i] = 0;
51}
52my $m = 0xff;
53for (my $i = 8; $i > 0; $i--) {
54    $bits_in_quad[$m] = $i;
55    $m <<= 1;
56    $m &= 0xfe;
57}
58
59
60if ( !-x $OSPFD) {
61    if (-x "/usr/bin/apt-get" ) {
62        system("/usr/bin/apt-get", "-y", "install", "quagga");
63    }
64    elsif (-x "/usr/bin/yum") {
65        system("/usr/bin/yum", "-y", "install", "quagga");
66    }
67}
68
69chomp (my $cif = `/usr/local/etc/emulab/control_interface`);
70chomp (my $hostname = `hostname`);
71$hostname =~ s/\..*//;
72
73my $ifp = new IO::Pipe();
74$ifp->reader("$IFCONFIG");
75my $current = undef;
76while (<$ifp>) {
77    /^(\S+)/ && do {
78        my $ifname = $1;
79        if ($ifname ne $cif && $ifname !~ "^lo" ) {
80            push(@live_ones, $ifname);
81            $current = $ifname;
82        }
83        else { $current = undef; }
84        print "$ifname $current\n";
85    };
86    /inet addr:\s*(\S+)\s*\S*\s*mask:\s*(\S+)/i && do {
87        next unless $current;
88        print "$current $1 $2\n";
89        my $slash = 0;
90        foreach my $quad (split(/\./, $2)) {
91            if ($bits_in_quad[$quad] != 0 ) { $slash += $bits_in_quad[$quad];}
92            else { last;}
93        }
94        $ainfo{$current} = [ $1, $slash ];
95        print $current, " ", @{$ainfo{$current}}, "\n";
96    }
97}
98$ifp->close();
99
100my $f = new IO::File(">$zebrafn") || die "Can't open $zebrafn: $!\n";
101print $f "hostname $hostname\n";
102foreach my $i (@live_ones) {
103    print $f "interface $i\n";
104}
105$f->close();
106my $f = new IO::File(">$ospffn") || die "Can't open $ospffn: $!\n";
107print $f "hostname $hostname\n";
108print $f "router ospf\n";
109foreach my $i (@live_ones) {
110    print $f " network ";
111    print $f netslash($ainfo{$i}->[0], $ainfo{$i}->[1]),"/",$ainfo{$i}->[1]," area 0.0.0.2\n";
112}
113$f->close();
114foreach my $fn ($zebrafn, $ospffn) {
115    system("chmod 644 $fn");
116}
117$f = new IO::File(">/proc/sys/net/ipv4/ip_forward") || 
118    die "Can't open sys forwarding file:$!\n";
119print $f "1\n";
120$f->close();
121system("$ZEBRA -d -f $zebrafn");
122system("$OSPFD -d -f $ospffn");
123
Note: See TracBrowser for help on using the repository browser.