source: fedkit/config_from_tunnelip.pl @ 6a8a9ec

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since 6a8a9ec was 8c9933c, checked in by Ted Faber <faber@…>, 15 years ago

tweak to make the routerdest behavior a littel neater.

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[53c649f]1#!/usr/bin/perl
2
3use strict;
4use IO::Pipe;
5use Getopt::Long;
6
[fde165c]7# Commands to use below.  These all seem to be in the same place on FreeBSD and
8# Lunix
[53c649f]9my $TMCC = "/usr/local/etc/emulab/tmcc";
10my $FINDIF = "/usr/local/etc/emulab/findif";
11my $IFCONFIG = "/sbin/ifconfig";
12my $ROUTE = "/sbin/route";
13
[fde165c]14my $tmcc = new IO::Pipe || die "Can't create tmcc pipe: $!\n";  # To parse tmcc
15my $interface;                  # Interface with external address
16my $ip;                         # IP address of external interface
17my $mac;                        # MAC address
18my $netmask;                    # Netmask
19my $router;                     # Router for the internet
20my $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]23my $os = `uname`;
24chomp $os;
25
[fde165c]26# Option parsing, --destination sets $routedest
[53c649f]27GetOptions('destination=s' => \$routedest);
28
[fde165c]29# Parse out the info about tunnelips
[53c649f]30$tmcc->reader("$TMCC tunnelip");
31while (<$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
40die "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`;
44chomp $interface;
45die "Can't get interface for mac address $mac: $?" if $? || !$interface;
46
[fde165c]47# Do the ifconfig
[53c649f]48my @ifconfig = ($IFCONFIG, $interface, $ip);
49push(@ifconfig, 'netmask', $netmask) if $netmask;
50
51system(@ifconfig);
52die join(" ", @ifconfig) . " failed: $!\n" if $?;
53
[fde165c]54# Add the host route, if needed
[8c9933c]55if ($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]72else { warn "Destination but no router\n" if $routedest; }
[53c649f]73exit(0);
Note: See TracBrowser for help on using the repository browser.