source: fedkit/config_from_tunnelip.pl @ b06744b

Last change on this file since b06744b was 84623c3, checked in by Ted Faber <faber@…>, 12 years ago

IP address override

  • Property mode set to 100644
File size: 2.5 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
[84623c3]23my $forceip;                    # User overriddedn IP
24my $forcemask;                  # User overriddedn netmask
[53c649f]25
[fde165c]26# Linux and FreeBSD use slightly different route syntax, so get the OS
[53c649f]27my $os = `uname`;
28chomp $os;
29
[fde165c]30# Option parsing, --destination sets $routedest
[84623c3]31GetOptions('destination=s' => \$routedest, "record=s" => \$recordfile,
32    'forceip=s' => \$forceip, 'forcemask=s' => \$forcemask);
[53c649f]33
[fde165c]34# Parse out the info about tunnelips
[53c649f]35$tmcc->reader("$TMCC tunnelip");
36while (<$tmcc>) {
37    chomp;
38    /TUNNELIP=([\d\.]*)/ && do { $ip = $1; };
39    /TUNNELMASK=([\d\.]*)/ && do { $netmask = $1; };
40    /TUNNELMAC=([[:xdigit:]]*)/ && do { $mac = $1; };
41    /TUNNELROUTER=([\d\.]*)/ && do { $router = $1; };
42}
43$tmcc->close();
44
45die "No MAC information for tunnel.\n" unless $mac;
46
[84623c3]47# Let the user override the IP and mask if they know what they are doing.
48$ip = $forceip if $forceip;
49$netmask = $forcemask if $forcemask;
50
[fde165c]51# Use the emulab findif command to get the right interface to configure
[53c649f]52$interface = `$FINDIF $mac`;
53chomp $interface;
54die "Can't get interface for mac address $mac: $?" if $? || !$interface;
55
[fde165c]56# Do the ifconfig
[53c649f]57my @ifconfig = ($IFCONFIG, $interface, $ip);
58push(@ifconfig, 'netmask', $netmask) if $netmask;
59
60system(@ifconfig);
61die join(" ", @ifconfig) . " failed: $!\n" if $?;
62
[fde165c]63# Add the host route, if needed
[8c9933c]64if ($router) {
65    if ($routedest ) {
[53c649f]66        my @cmd;
67
68        if ( $os =~ /^Linux/ ) { 
69            @cmd = ($ROUTE, 'add', $routedest, 'gw', $router);
70        }
71        elsif ( $os =~ /^FreeBSD/ ) {
72            @cmd = ($ROUTE, 'add', $routedest, $router);
73        }
74        else {
75            die "Unknown OS: $os\n";
76        }
77        system(@cmd);
78        die join(" ", @cmd) . " failed: $?\n" if $?;
79    }
80}
[8c9933c]81else { warn "Destination but no router\n" if $routedest; }
[dc803a7]82if ($recordfile) {
83    $netmask = "255.255.255.0" unless $netmask;
84    my $rf = new IO::File(">>$recordfile") || 
85        die "Can't open $recordfile:$!\n";
86    print $rf "$ip:$interface:$mac:$netmask\n";
87    $rf->close();
88}
[53c649f]89exit(0);
Note: See TracBrowser for help on using the repository browser.