How to do reverse DNS lookups in Java

I have a list of IP addresses. I need to do reveres DNS, i.e. I want a website name.

I tried the following:

InetAddress addr = InetAddress.getByName("98.138.253.109"); String host = addr.getCanonicalHostName(); System.out.println(host); 

But, the IP in my example for yahoo.com, when I run the code, I get: ir1.fp.vip.ne1.yahoo.com

I need a way so that I can get the domain name by entering the IP address.

+4
source share
2 answers

The IP address in the code you provided resolves yahoo because it is held by yahoo.

The IP address you use is for one host only. As you can imagine, Yahoo probably has thousands of servers. They are assigned allocation ipaddresses, which are then reused as they see fit for internal distribution. Different servers under the yahoo domain have different names, and therefore, when you refer to the IP address from a specific server, you get a name for that specific server. For a generic ipaddress for yahoo.com, which can be hidden if you do not want to use nslookup and request a series of ipaddress to find yahoo.com distributions and generalize from there.

+4
source

Using an IP address to obtain the domain name (as you expect it) may not be possible. There will be several servers for Yahoo, and all of them will be displayed on the yahoo.com domain name. But they will have an individual IP address and host name. The addr.getCanonicalHostName () function returns the host name, not the domain name!

+4
source

All Articles