source: fedkit/config_from_tunnelip.pl @ 2edec46

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since 2edec46 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
Line 
1#!/usr/bin/perl
2
3use strict;
4use IO::Pipe;
5use Getopt::Long;
6
7# Commands to use below.  These all seem to be in the same place on FreeBSD and
8# Lunix
9my $TMCC = "/usr/local/etc/emulab/tmcc";
10my $FINDIF = "/usr/local/etc/emulab/findif";
11my $IFCONFIG = "/sbin/ifconfig";
12my $ROUTE = "/sbin/route";
13
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)
21
22# Linux and FreeBSD use slightly different route syntax, so get the OS
23my $os = `uname`;
24chomp $os;
25
26# Option parsing, --destination sets $routedest
27GetOptions('destination=s' => \$routedest);
28
29# Parse out the info about tunnelips
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
42# Use the emulab findif command to get the right interface to configure
43$interface = `$FINDIF $mac`;
44chomp $interface;
45die "Can't get interface for mac address $mac: $?" if $? || !$interface;
46
47# Do the ifconfig
48my @ifconfig = ($IFCONFIG, $interface, $ip);
49push(@ifconfig, 'netmask', $netmask) if $netmask;
50
51system(@ifconfig);
52die join(" ", @ifconfig) . " failed: $!\n" if $?;
53
54# Add the host route, if needed
55if ($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}
72else { warn "Destination but no router\n" if $routedest; }
73exit(0);
Note: See TracBrowser for help on using the repository browser.