Because what I wanted to do was dynamically get the localhost, this is for scripts that will run on multiple machines, and then use that to get the IPv6 address I came up with the following. I put the print statements in just so I can check that I have things following the right paths for various machines, and right now once I get the address the script exits. After I test it on a sampling of machines I will update this to just send the IP address back, the regular expression still needs work, but I haven't found a good way to pull IPv6 formatted addresses yet. When I do, I'll update this.
sub getHostIPv6()
{
my $hostName = Net::Domain::hostfqdn();
my $ip;
my @rawIpData;
print "Found $hostName\n";
if ($^O =~ m/Win/)
{
print "Running $^O - Win\n";
@rawIpData = `nslookup -q=AAAA $hostName`;
}
elsif ($^O =~ m/solaris/)
{
print "Running $^O - Solaris\n";
@rawIpData = `ifconfig -a`;
}
else
{
print "Running $^O - Unix\n";
# Pull out the matching ip
@rawIpData = `host -avt AAAA $hostName`;
}
foreach my $line (@rawIpData)
{
if ($line =~ m/AAAA/ && $line =~ m/IPv6/)
{
$line =~ s/^.*IPv6\saddress\s=\s//;
chomp($line);
# Return IPv6 IP
$ip = $line;
}
elsif ($line =~ m/inet6/)
{
$line =~ s/^\s+inet6\s+//;
chomp($line);
# Return IPv6 IP
$ip = $line;
}
}
if ($ip eq "")
{
print "Did not find an IPv6 address on the $^O machine\n";
exit(1);
}
print "Found IP-$ip.\n";
exit(1);
}