Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Pulling IPv6 Address from Hostname

by gokuraku (Monk)
on Mar 12, 2008 at 19:22 UTC ( #673800=perlquestion: print w/ replies, xml ) Need Help??
gokuraku has asked for the wisdom of the Perl Monks concerning the following question:

I've been looking at this one for awhile, and the question is simple (or should be but I can't seem to get a good solution), if I get the hostname
$hostName = Net::Domain::hostfqdn()
I want to use that and get the IPv6 formatted address (the hex one with the :'s) next, but I am unclear how to do that. Working with NetAddr::IP::Util I would think I can just translate it, but I can't quite get the right one. I thought I might be able to use ipv6_aton() and translate it to an RDATA string and work it from there, or even use the $hostName of something like mymachine.network.net in inet_any2n() to get a 128 bit address, but I keep coming up with empty values.
Would anyone who has done this before be able to give me some hints on how to just get the textual address into the he format? Or anyone who can guide me to a nice solution, it would be appreciated.
Thanks!

Comment on Pulling IPv6 Address from Hostname
Download Code
Re: Pulling IPv6 Address from Hostname
by pc88mxer (Vicar) on Mar 12, 2008 at 20:56 UTC
    NetAddr::Ip::Util doesn't actually perform any look-ups. It just helps you convert between the different representations of ip address (ascii vs. binary, IPv4 vs. IPv6).

    The basic way to find a host's IPv6 address is to use nslookup and query the AAAA record. For instance:

    $ nslookup -q=AAAA pnrpv2.ipv6.microsoft.com ...(my DNS server details omitted)... Non-authoritative answer: pnrpv2.ipv6.microsoft.com has AAAA address 2002:4136:e383::4136: +e383 $

    In theory, you can perform a reverse look-up for this IPv6 address by using:

    $ nslookup -q=PTR 3.8.3.e.6.3.1.4.3.8.3.e.6.3.1.4.2.0.0.2.ip6.arpa.
    Each hex digit of the IPv6 address is separated by a dot (.) and is specified in reverse order.
Re: Pulling IPv6 Address from Hostname
by idsfa (Vicar) on Mar 13, 2008 at 14:03 UTC
    use Net::DNS; my $res = Net::DNS::Resolver->new; my $query = $res->query("example.com", "AAAA"); if ($query) { foreach $rr (grep { $_->type eq 'AAAA' } $query->answer) { print $rr->address, "\n"; } } else { warn "query failed: ", $res->errorstring, "\n"; }

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
Re: Pulling IPv6 Address from Hostname
by Khen1950fx (Canon) on Mar 13, 2008 at 21:06 UTC
    To expand on what pc88mxer gave you, you can use the command "host" instead of "nslookup":

    #!/usr/bin/perl use strict; use warnings; my $ns = system('host -avt AAAA pnrpv2.ipv6.microsoft.com'); print $ns, "\n";
      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); }
        For host, try:

        host -it AAAA pnrpv2.ipv6.microsoft.com

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://673800]
Approved by Erez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-09-16 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls