Wednesday, June 25, 2008

Find the host/computer name from its IP & vice versa

Here is a small snippet of PERL code to display a host/computer's name from its IP address.

use Socket;
$iaddr = inet_aton("206.190.60.37");
$name = gethostbyaddr($iaddr, AF_INET);
print "$name\n";

Running this code will give the host name as w2.rc.vip.re4.yahoo.com

Similarly, you can get the IP address of a host/computer whose name is known.
Below is the code for that:

use Socket;
$iaddr = gethostbyname("www.google.co.in");
$addr = inet_ntoa($iaddr);
print "$addr\n";

P S -
Your computer should be connected to the internet, to try these example.

0 comments: