Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

IP address to Hostname

by sanPerl (Friar)
on Jul 02, 2009 at 21:07 UTC ( [id://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 69.147.76.15 then it should reurn me www.yahoo.com. Any suggestions please?

Update
After looking into gethostbyaddr help. I tried following to get "www.yahoo.com" (and Not "f1.us.www.vip.ird.yahoo.com").
my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyaddr(inet_aton +("69.147.76.15"), AF_INET); print $name."\n"; print $aliases."#".$addrtype."#".$length."\n";
I am expecting "www.yahoo.com" 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 (Patriarch) on Jul 02, 2009 at 21:28 UTC

    It's actually f1.www.vip.re1.yahoo.com. www.yahoo.com 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 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 toolic (Bishop) on Jul 02, 2009 at 21:14 UTC
Re: IP address to Hostname
by imrags (Monk) on Jul 03, 2009 at 09:52 UTC
    TIMTOWTDI. :)
    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.
    Raghu
Re: IP address to Hostname
by ikegami (Patriarch) on Jul 03, 2009 at 16:34 UTC

    Regarding your update,

    I tried following to get "www.yahoo.com" (and Not "f1.us.www.vip.ird.yahoo.com")

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

    $ nslookup -norecurse www.yahoo.com Non-authoritative answer: www.yahoo.com canonical name = www.wa1.b.yahoo.com.

    www.yahoo.com 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found