System notes



reverse.name.lookup

Version date: Thu May 24 14:28:12 EDT 2012

Q: Can anyone tell me how to use nslookup (or some other tool) to get
a hostname from an internet address.

You need to tell it to ask for a "PTR" record type by setting the querytype
variable to "ptr". Then for address a.b.c.d, ask for d.c.b.a.in-addr.arpa
(yes, in reverse). For example:

-----------------------------------------
% nslookup
Default Server: sparkyfs.itstd.sri.com
Address: 128.18.4.39

> ee.umr.edu <--- will get A record
Server: sparkyfs.itstd.sri.com
Address: 128.18.4.39

Non-authoritative answer:
Name: ee.umr.edu
Address: 131.151.4.1

> set q=ptr <--- ask for PTR record
> 1.4.151.131.in-addr.arpa <--- look up address
Server: sparkyfs.itstd.sri.com
Address: 128.18.4.39

1.4.151.131.in-addr.arpa name = umree.ee.umr.edu
>
-----------------------------------------

Try this perl script. I like this one the best because you can use if for
either domain names or IP addresses. Though, it would not be hard to modify the above
sh script to check for this.

-------------------
#!/usr/local/bin/perl
# get information about a host based on either its name or IP address
&gethostinfo($ARGV[0]);

if ( $host ) {
print "Name: $host\n";
foreach $address (@addresses) {
printf "Address: %s\n",join(".",unpack("C4",$address));
}
} else {
print "$ARGV[0]: Not found!\n";
}

sub gethostinfo {
local($name_or_addr) = $_[0];
if ($name_or_addr =~ /^\s*[a-zA-Z]/) {
($host,$aliases,$type,$length,@addresses) =
gethostbyname($name_or_addr);
}
else {
local($AF_INET) = 2;
local($address) = pack("C4",split(/\./,$name_or_addr));
($host,$aliases,$type,$length,@addresses) =
gethostbyaddr($address,$AF_INET);
}
$host;
}
-------------------


Please read The Standard Disclaimer regarding information found in these pages.

To return to the top of the Corcoran document archives Click Here

If you came here directly from a search engine, and would like to view these pages in the proper frames from the top, Start Here

Or start at the very top of Mike Corcoran's home page