marknm has asked for the wisdom of the Perl Monks concerning the following question:

I would like to be able to find the aliases for a hostname given the actual hostname. For example, if I pass an alias to Net::hostent's functions it will return the hostname. What I would like is to be able to pass the hostname in and get back the aliases. I can find many ways to go from the alias to the hostname but no way to get from the hostname to the alias. Is this something I should be able to do? What am I missing? Thanks for your help - Mark

Replies are listed 'Best First'.
Re: Finding hostname aliases
by kvale (Monsignor) on Sep 11, 2003 at 22:33 UTC
    One way to get some aliases is to use Net::hostent. Here is an example from the manpage:
    use Net::hostent; use Socket; @ARGV = ('netscape.com') unless @ARGV; for $host ( @ARGV ) { unless ($h = gethost($host)) { warn "$0: no such host: $host\n"; next; } printf "\n%s is %s%s\n", $host, lc($h->name) eq lc($host) ? "" : "*really* ", $h->name; print "\taliases are ", join(", ", @{$h->aliases}), "\n" if @{$h->aliases}; if ( @{$h->addr_list} > 1 ) { my $i; for $addr ( @{$h->addr_list} ) { printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($add +r); } } else { printf "\taddress is [%s]\n", inet_ntoa($h->addr); } if ($h = gethostbyaddr($h->addr)) { if (lc($h->name) ne lc($host)) { printf "\tThat addr reverses to host %s!\n", $h->na +me; $host = $h->name; redo; } } }

    -Mark

      And then there are wildcard DNS records. Try the above code (assuming the script is called "resolve") with:
      $ perl resolve www.startpagina.nl
      which gives:
      www.startpagina.nl is www.startpagina.nl addr #0 is [62.69.162.8] addr #1 is [62.69.162.6] addr #2 is [62.69.162.7]
      All nice and good, you would say. You get 3 IP-numbers and it resolves back correctly. Now you want to find all the aliases to "www.startpagina.nl". Try this by doing:
      $ perl resolve anythingyoucanthinkof.startpagina.nl
      which generates this output:
      anythingyoucanthinkof.startpagina.nl is anythingyoucanthinkof.startpag +ina.nl addr #0 is [62.69.162.7] addr #1 is [62.69.162.8] addr #2 is [62.69.162.6] That addr reverses to host www.startpagina.nl! www.startpagina.nl is www.startpagina.nl addr #0 is [62.69.162.8] addr #1 is [62.69.162.6] addr #2 is [62.69.162.7]
      Now replace "anythingyoucanthinkof" with anything else you can think of. Then meditate on the number of aliases "www.startpagina.nl" has. ;-)

      Hope this helps.

      Liz
        Thanks for all the pointers - now understand a lot more about how to properly use Net::hostent. However, my problem still remains. For instance, if I use the code snippet supplied my shenme, I get exactly the output promised. But my problem is that I would like to be able to give 'a562.cd.akamai.net' as the argument and have it return 'www.microsoft.com' as an alias. Alas, this does not happen and it appears that this is indeed the way this module (Net::hostent) is intended to work. So I am still wandering in the dark :( Thanks for all your efforts - Mark
      Works for me, along with the original gethostbyname().
      use Net::hostent; my $h = gethost( 'www.microsoft.com' ); # name aliases addrtype length addr_list my @aliases = @{ $h->aliases }; printf " Name '%s'\n", $h->name; printf " Aliases are '%s'\n", join("', '", @aliases );
      Output:
        Name  'a562.cd.akamai.net'
        Aliases are 'www.microsoft.com', 'www.microsoft.com.edgesuite.net'
      
Re: Finding hostname aliases
by liz (Monsignor) on Sep 11, 2003 at 22:26 UTC
    Not that this has anything to do with Perl really. But anyway, what can you do:

    1. A hostname A can give you an IP-number if it resolves.
    2. A reverse lookup of the IP-number may give you a hostname B if it resolves.
    3. A and B may or may not be the same.

    You should think of hostnames as nothing other than a "roadmap" to get to a specific (IP) address. And there are an infinite number of ways to Rome: some of them are described (have a hostname) and some are not described. But you will never be able to find them all, unless someone (else) tells them to you.

    Hope this helps.

    Liz