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

I need to find the name of the server from an ip address. Currently I can find the IP address of a server name but want to reverse this and find the server name from an IP address that I have. Please advise. Here is a script to fetch ip address from server name:
foreach $host (@ARGV) { ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($host); #gethostbyname IS A PERL print "$host:\n"; foreach $a (@addrs) { $i = join(".", unpack("C4", $a)); if( ! $i ) { print ">>>>>>$i<<<<<<\n"; print "NOT FOUND\n"; } else { print "$i\n"; } } }

Replies are listed 'Best First'.
Re: Get server name from ip address
by Fletch (Bishop) on Apr 09, 2004 at 17:24 UTC
Re: Get server name from ip address
by eXile (Priest) on Apr 09, 2004 at 17:52 UTC
    A note on using name to IP / IP to name translations using DNS:

    All answers you get from these translations depend on how the specific DNS domains are set up. Without going into details, when doing a name to IP translation and then doing a IP to name translation (a so called 'reverse lookup') you might end up with a different name than you started with. For instance for 'www.perlmonks.org' your scipt will output:
    209.197.123.153 66.39.54.27
    When doing a reverse lookup on these addresses (I used the 'dig -x <addr>' to do this) you'll get:
    perlmonks.pair.com
    and
    ads.perlmonks.org
    This is because forward and reverse DNS don't have to be the same. And to complicate things even further DNS-records change in time. So an answer you got for a 'gethostbyname' query today can be different from an answer you got yesterday.