source: fedkit/config_from_tunnelip.pl @ ef7d689

compt_changes
Last change on this file since ef7d689 was dc803a7, checked in by Ted Faber <faber@…>, 15 years ago

Set up all routing daemons from the fedkit and note interfaces we've added in a way that SEER's federated testbed will grok them.

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