gen-ip-mapping.pl 2.15 KB
Newer Older
1
#!/usr/bin/perl -w
Mike Hibler's avatar
Mike Hibler committed
2 3 4 5 6
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
7 8 9 10 11

#
# Make an ipmap file from /etc/hosts
#

12 13 14 15 16
use Getopt::Std;

my %opt;
getopts('p',\%opt);

17 18 19 20 21 22 23 24
use strict;
my (%elabips, %plabips);

open(HOSTS,"/etc/hosts") or die "Unable to open /etc/hosts: $!\n";

while (<HOSTS>) {
    my ($IP,@names) = split /\s+/;
    my $firstname = shift @names;
25
    if ($firstname =~ /^(.+)-(elink|elan|ecloud|elabc)$/) {
26
        $elabips{$1} = $IP;
27
    } elsif ($firstname =~ /^(.+)-(plink|plan|pcloud|plabc)$/) {
28 29 30 31
        $plabips{$1} = $IP;
    }
}

32
my $lines_output = 0;
33 34
foreach my $elabnode (keys %elabips) {
    my $plabnode = $elabnode;
35 36 37 38 39 40 41 42 43
    my $planetnode = $elabnode;
    $plabnode =~ s/elab/plab/i;
    $planetnode =~ s/elab/planet/i;
    #if ($opt{p}) {
    #    $plabnode =~ s/elab/planet/i;
    #} else {
    #    $plabnode =~ s/elab/plab/i;
    #}
    if (exists $plabips{$plabnode}&& !$opt{p}) {
44
        print "$elabips{$elabnode} $plabips{$plabnode} elabc-$elabnode\n";
45 46 47
        $lines_output++;
    } elsif (exists $plabips{$planetnode}&& !$opt{p}) {
        print "$elabips{$elabnode} $plabips{$planetnode} elabc-$elabnode\n";
48
        $lines_output++;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    } else {
        # Let's hope this is an experiment with a real (not emulated) planetlab
        # half. Note - this should be run by something that has sourced
        # common-env.sh so that these are set
        my $plabhostname = "$elabnode.$ENV{EXPERIMENT}.$ENV{PROJECT}.emulab.net";
        $plabhostname =~ s/^elab/planet/;
        # Find out the IP address for $plabhostname
        open(H,"host $plabhostname |") or die "Unable to run host\n";
        # Yuck. Why can't 'host' have a flag to spit out just the IP?
        <H>; # Throw away first line
        # The next line is the one with interesting data
        my $interesting_line = <H>;
        close H;
        if ($interesting_line =~ /has address (.*)$/) {
            my $plabIP = $1;
            print "$elabips{$elabnode} $plabIP elabc-$elabnode\n";
            $lines_output++;
        }
67 68 69 70
    }
}

close HOSTS;
71 72 73 74 75 76 77

if ($lines_output == 0) {
    warn "Hosts file had no names I recognize\n";
    exit 1;
}

exit 0;