1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use IO::Pipe; |
---|
5 | use Getopt::Long; |
---|
6 | |
---|
7 | # Commands to use below. These all seem to be in the same place on FreeBSD and |
---|
8 | # Lunix |
---|
9 | my $TMCC = "/usr/local/etc/emulab/tmcc"; |
---|
10 | my $FINDIF = "/usr/local/etc/emulab/findif"; |
---|
11 | my $IFCONFIG = "/sbin/ifconfig"; |
---|
12 | my $ROUTE = "/sbin/route"; |
---|
13 | |
---|
14 | my $tmcc = new IO::Pipe || die "Can't create tmcc pipe: $!\n"; # To parse tmcc |
---|
15 | my $interface; # Interface with external address |
---|
16 | my $ip; # IP address of external interface |
---|
17 | my $mac; # MAC address |
---|
18 | my $netmask; # Netmask |
---|
19 | my $router; # Router for the internet |
---|
20 | my $routedest; # Add host route to this address (if given) |
---|
21 | |
---|
22 | # Linux and FreeBSD use slightly different route syntax, so get the OS |
---|
23 | my $os = `uname`; |
---|
24 | chomp $os; |
---|
25 | |
---|
26 | # Option parsing, --destination sets $routedest |
---|
27 | GetOptions('destination=s' => \$routedest); |
---|
28 | |
---|
29 | # Parse out the info about tunnelips |
---|
30 | $tmcc->reader("$TMCC tunnelip"); |
---|
31 | while (<$tmcc>) { |
---|
32 | chomp; |
---|
33 | /TUNNELIP=([\d\.]*)/ && do { $ip = $1; }; |
---|
34 | /TUNNELMASK=([\d\.]*)/ && do { $netmask = $1; }; |
---|
35 | /TUNNELMAC=([[:xdigit:]]*)/ && do { $mac = $1; }; |
---|
36 | /TUNNELROUTER=([\d\.]*)/ && do { $router = $1; }; |
---|
37 | } |
---|
38 | $tmcc->close(); |
---|
39 | |
---|
40 | die "No MAC information for tunnel.\n" unless $mac; |
---|
41 | |
---|
42 | # Use the emulab findif command to get the right interface to configure |
---|
43 | $interface = `$FINDIF $mac`; |
---|
44 | chomp $interface; |
---|
45 | die "Can't get interface for mac address $mac: $?" if $? || !$interface; |
---|
46 | |
---|
47 | # Do the ifconfig |
---|
48 | my @ifconfig = ($IFCONFIG, $interface, $ip); |
---|
49 | push(@ifconfig, 'netmask', $netmask) if $netmask; |
---|
50 | |
---|
51 | system(@ifconfig); |
---|
52 | die join(" ", @ifconfig) . " failed: $!\n" if $?; |
---|
53 | |
---|
54 | # Add the host route, if needed |
---|
55 | if ($router) { |
---|
56 | if ($routedest ) { |
---|
57 | my @cmd; |
---|
58 | |
---|
59 | if ( $os =~ /^Linux/ ) { |
---|
60 | @cmd = ($ROUTE, 'add', $routedest, 'gw', $router); |
---|
61 | } |
---|
62 | elsif ( $os =~ /^FreeBSD/ ) { |
---|
63 | @cmd = ($ROUTE, 'add', $routedest, $router); |
---|
64 | } |
---|
65 | else { |
---|
66 | die "Unknown OS: $os\n"; |
---|
67 | } |
---|
68 | system(@cmd); |
---|
69 | die join(" ", @cmd) . " failed: $?\n" if $?; |
---|
70 | } |
---|
71 | } |
---|
72 | else { warn "Destination but no router\n" if $routedest; } |
---|
73 | exit(0); |
---|