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