Created
February 4, 2014 21:42
-
-
Save xy4n/8812929 to your computer and use it in GitHub Desktop.
gen_hosts.pl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# NO FITNESS FOR A PARTICULAR PURPOSE. NO MERCHANTABILITY. | |
unless ($ARGV[0]) { | |
die "Usage: gen_hosts.pl <hosts_file>\n"; | |
} | |
my $hosts_file = $ARGV[0]; | |
my $hostmap; | |
# load existing entries into the host map | |
if (-e $hosts_file) { | |
open my $hf, '<', $hosts_file; | |
while (my $line = <$hf>) { | |
next if $line =~ /^#/; | |
my @cols = split(/\s+/, $line); | |
push(@{$hostmap->{$cols[0]}}, @cols[1..$#cols]); | |
} | |
close($hf); | |
} | |
# get the hosts data out of the nodeinfo database, must be connected to hyperboria! | |
my $hosts_url = "http://[fc5d:baa5:61fc:6ffd:9554:67f0:e290:7535]/nodes/list.perl"; | |
my $data = `curl -s -g $hosts_url`; | |
if ($data =~ /^\[/ && $data =~ /\]$/) { | |
my $ar = eval($data); | |
foreach my $entry (@$ar) { | |
# don't include entries that have the same name as the IP or are just the las | |
# 4 of the ipv6 or names that contain illegal chars. | |
if (length($entry->{name}) => 3 && | |
$entry->{ip} ne $entry->{name} && | |
$entry->{name} !~ /(?:^[0-9a-f]{4}$|[_\s]+|^-)/) { | |
$hostmap->{$entry->{ip}} = [$entry->{name}]; | |
} | |
} | |
open my $hf, '>', $hosts_file; | |
print $hf "# hosts file automatically generated by gen_hosts.pl\n\n"; | |
foreach my $addr (keys %$hostmap) { | |
# one name for every line. | |
foreach my $col (@{$hostmap->{$addr}}) { | |
print $hf "$addr $col\n"; | |
} | |
} | |
print "[done]: wrote " . scalar(keys %$hostmap) . " entries\n"; | |
} else { | |
die "[fatal]: bad data\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment