I have a requirement wherein an IP address can have multiple hostnames mapped to it. I tried looking into InetAddress.getAllByName("10.33.28.55")
however I did not get the desired result, it returned just one entry. nslookup
on the IP address returns all DNS entries. How do I retrieve all the hostnames associated with this IP address in Java?

- 5,376
- 7
- 34
- 49
-
Are you running `nslookup` on the same machine as your Java code? `InetAddress.getAllByName()` *should* do what you're asking. – Brian Roach May 17 '13 at 18:11
-
@BrianRoach: Initially I did not, however, when I ran `InetAddress.getAllByName()` on the same machine as `nslookup` it still returned 1 entry in the array, and I am expecting 2. Interestingly, the `getCanonicalHostName()` and `getHostName()` functions returned 2 different values. I was expecting these 2 values would come up differently as 2 elements in the array. – devang May 17 '13 at 18:59
-
2Actually ... looking at the source for `InetAddress.getAllByName()` ... if it's an IP address, it doesn't actually do a lookup, heh. You get back a single `InetAddress` object that contains the IP you provided. Learn something new every day; I don't think I've ever tried that. – Brian Roach May 17 '13 at 19:06
-
@BrianRoach: That is interesting. Let me see what it returns if I use hostname. – devang May 17 '13 at 19:11
-
I believe I've figured out what you need, see my revised answer. – Brian Roach May 17 '13 at 19:42
-
I don't know java, so this is not the answer u looking for. in linux you can check the manpage gethostbyname, which is in Posix standard . There's "h_addr_list" in the returned struct hostent. in python you can have _ip, _alias, ips = socket.gethostbyname_ex (name) which ips is a list, and _ip is one entry of the list – frostyplanet May 18 '13 at 16:09
3 Answers
Looking at the source code for InetAddress.getAllByName()
you find that it doesn't actually do a DNS query if the provided String
is textual representation of an IP address. It simply returns an array containing a single InetAdddress
object containing the IP. They even put a handy comment right in the method:
// if host is an IP address, we won't do further lookup
(See: http://javasourcecode.org/html/open-source/jdk/jdk-6u23/java.net/InetAddress.java.html)
If only the JavaDoc was so clear. It states "If a literal IP address is supplied, only the validity of the address format is checked." ... I would argue that doesn't tell you that it isn't going to be looked up.
Thinking about it, however ... it makes sense in the context of InetAddress
- the class encapsulates an IP address of which ... you only have one. It really needs getHostNames()
and getAllCanonicalNames()
(note the plurality) methods that would do what you are asking. I'm thinking of opening an issue / submitting a patch.
That said, it would appear currently there's no built in method of doing a RDNS query where multiple PTR records are supported. All the other lookup methods simply lop off the first record returned and that's what you get.
You're going to have to look into 3rd party DNS libraries for java (sorry, I don't have experience with using any of them).
Edit to add: I like figuring things out. I do not have an IP handy that has multiple PTR records to test this against, but it should do the trick.
import java.io.IOException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
public class App
{
public static void main(String[] args) throws IOException, NamingException
{
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
InitialDirContext idc = new InitialDirContext(env);
String ipAddr = "74.125.225.196";
// Turn the IP into an in-addr.arpa name
// 196.225.125.74.in-addr.arpa.
String[] quads = ipAddr.split("\\.");
StringBuilder sb = new StringBuilder();
for (int i = quads.length - 1; i >= 0; i--)
{
sb.append(quads[i]).append(".");
}
sb.append("in-addr.arpa.");
ipAddr = sb.toString();
Attributes attrs = idc.getAttributes(ipAddr, new String[] {"PTR"});
Attribute attr = attrs.get("PTR");
if (attr != null)
{
for (int i = 0; i < attr.size(); i++)
{
System.out.println((String)attr.get(i));
}
}
}
}

- 76,169
- 12
- 136
- 161
-
That is awesome stuff. However, I went back to creating a small script to do the stuff for me. Thanks a lot. – devang May 17 '13 at 21:04
-
-
Very good but I think you could have acknowledged the source of the idea to use JNDI. – user207421 May 19 '13 at 10:05
-
I have added entry in host file and tried executing javax.naming.NameNotFoundException: DNS name not found [response code 3]. – Santoshkumar Kalasa Nov 11 '20 at 06:44
Well, there is only one good way: call nslookup or dig or whatever from the Java process.
With Runtime.getRuntime().exec(..)
or better with ProcessBuilder...

- 445
- 3
- 7
-
I am aware about this, I was trying to avoid that so that I do not have to do the parsing and rely on Java to return that. It seems I will have to use this approach, use `nslookup` or `host` commands to get the list and do the necessary parsing. – devang May 17 '13 at 19:00
This answer might be helpful: https://stackoverflow.com/a/24205035/8026752
Using the lookupAllHostAddr
method of DNSNameService
works for me, and returns all IP addresses by hostname. Maybe it will also help with finding all hostnames by IP address, but it seems this depends on DNS server configuration. In my case I even couldn't find all hostnames using nslookup
, so I couldn't test it, so I'm not sure about this solution.
One suggestion is that lookupAllHostAddr
is not static method, so you should use it like this:
InetAddress[] ipAddress = new DNSNameService().lookupAllHostAddr("hostname")
Also, from my perspective, this link could be interesting (it's also information from the same answer thread mentioned by me above, I just summarize it a bit):
https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html
On the linked page you can find properties to disable lookups caching:
sun.net.inetaddr.ttl
- you should add it to JVM start command line like this:-Dsun.net.inetaddr.ttl=0
, 0 here means that hostname will be cached for 0 secondsnetworkaddress.cache.ttl
- you should add the needed value to the java.security file located at%JRE%\lib\security
A bit more info can be found here also:

- 9,433
- 1
- 39
- 49

- 3
- 3