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

IP address to Hostname

by sanPerl (Friar)
on Jul 02, 2009 at 21:07 UTC ( #776850=perlquestion: print w/replies, xml ) Need Help??
sanPerl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I normlly use Net::DNS to get the ip address of hostname. But now I need Hostname from IP address. i.e if I provide then it should reurn me Any suggestions please?

After looking into gethostbyaddr help. I tried following to get "" (and Not "").
my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyaddr(inet_aton +(""), AF_INET); print $name."\n"; print $aliases."#".$addrtype."#".$length."\n";
I am expecting "" in the second line of output (i.e. $aliases), Am I doing something wrong?

Replies are listed 'Best First'.
Re: IP address to Hostname
by ikegami (Pope) on Jul 02, 2009 at 21:28 UTC

    It's actually is just an alias. gethostbyaddr is what you want.

    By the way, Net::DNS is overkill to resolve domains. Socket's inet_aton is sufficient.

      Not only is Net::DNS overkill, but using Net::DNS means you're committed to using DNS to do the domain name resolving. While that may work on many machines, gethostbyaddr will actually use the resolving that's configured for the system. Which, beside DNS, may consist of files, NIS, NIS+, LDAP, ... or some combination of resolving.
Re: IP address to Hostname
by toolic (Bishop) on Jul 02, 2009 at 21:14 UTC
Re: IP address to Hostname
by JavaFan (Canon) on Jul 02, 2009 at 21:17 UTC
    There's a Perl interface to the system interface by the same name, gethostbyaddr.
Re: IP address to Hostname
by imrags (Monk) on Jul 03, 2009 at 09:52 UTC
    1. you can use gethostbyaddr
    if($input =~ /^\d+.\d+.\d+.\d+$/) { my $name = gethostbyaddr(inet_aton($input),AF_INET); }
    Other way, complicated, but gives u more accurate information is
    2. Querying a website ARIN
    Gives you much more information by typing the IP in the "Who is" page.
Re: IP address to Hostname
by ikegami (Pope) on Jul 03, 2009 at 16:34 UTC

    Regarding your update,

    I tried following to get "" (and Not "")

    That's an odd request because is not associated with In fact, no IP address is.

    $ nslookup -norecurse Non-authoritative answer: canonical name = is just an alias to another domain. There's no way to find which domains are aliased to a domain name. In part, this is because anyone can alias a domain they own to any domain (even those they don't own).

      Thanks for letting me know this fact.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://776850]
Approved by toolic
[Corion]: Eily: Wheee ;)
[Eily]: I'll add that to my résumé
Discipulus what's SioB?
[Eily]: Saints in our Book
[Discipulus]: ..mmhh: no warnings; does not affect carp.. how to suppress carp's output in tests?

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-01-22 11:14 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.