[53c649f] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use IO::Pipe; |
---|
| 5 | use Getopt::Long; |
---|
| 6 | |
---|
[fde165c] | 7 | # Commands to use below. These all seem to be in the same place on FreeBSD and |
---|
| 8 | # Lunix |
---|
[53c649f] | 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 | |
---|
[fde165c] | 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) |
---|
[53c649f] | 21 | |
---|
[fde165c] | 22 | # Linux and FreeBSD use slightly different route syntax, so get the OS |
---|
[53c649f] | 23 | my $os = `uname`; |
---|
| 24 | chomp $os; |
---|
| 25 | |
---|
[fde165c] | 26 | # Option parsing, --destination sets $routedest |
---|
[53c649f] | 27 | GetOptions('destination=s' => \$routedest); |
---|
| 28 | |
---|
[fde165c] | 29 | # Parse out the info about tunnelips |
---|
[53c649f] | 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 | |
---|
[fde165c] | 42 | # Use the emulab findif command to get the right interface to configure |
---|
[53c649f] | 43 | $interface = `$FINDIF $mac`; |
---|
| 44 | chomp $interface; |
---|
| 45 | die "Can't get interface for mac address $mac: $?" if $? || !$interface; |
---|
| 46 | |
---|
[fde165c] | 47 | # Do the ifconfig |
---|
[53c649f] | 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 | |
---|
[fde165c] | 54 | # Add the host route, if needed |
---|
[8c9933c] | 55 | if ($router) { |
---|
| 56 | if ($routedest ) { |
---|
[53c649f] | 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 | } |
---|
[8c9933c] | 72 | else { warn "Destination but no router\n" if $routedest; } |
---|
[53c649f] | 73 | exit(0); |
---|