Here's the method-
public static String getHostByAddr(byte[] addr) throws UnknownHostException {
Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
final String[] servers = new String[] {"208.67.220.220", "208.67.222.222"};
final Resolver res = new ExtendedResolver(servers);
final Lookup lookUp = new Lookup(name, Type.PTR);
lookUp.setResolver(res);
Record[] records = lookUp.run();
if (records == null) {
throw new UnknownHostException();
}
return ((PTRRecord) records[0]).getTarget().toString();
}
And here's the call to the above method-
final InetAddress ip = InetAddress.getByName("198.154.218.168");
final byte[] bytes = ip.getAddress();
final String host = getHostByAddr(bytes);
System.out.println("Host - " + host);
Works fine for most of the cases, but fails when the IP is mapped to multiple domains (???)
Here's the example-
Get IP of securonix.com from here, it is 198.154.218.168
If I pass this IP to the above method it gives error
But if I try the same IP here, it lists down the 4 domains
Is it possible to do this with DNSJava?
In general, you can't. Just as the owner of the domain securonix.com created an entry on their DNS server that translates securonix.com to 198.154.218.168, the owner of the IP address 198.154.218.168 maintains a "reverse" DNS record (usually on a separate server) that maps the IP address to a default DNS name. See the page for "Reverse DNS lookup" on wikipedia for more information on reverse DNS lookups, and note that the reverse DNS lookup -- from IP address to name -- is often maintained by a different owner than the domain name owner and it is NOT just created "automatically" by swapping the name and IP address.
Furthermore, the site you mention doesn't seem to have reverse DNS entries for that IP address, so you won't ever get an answer for 198.154.218.168 from a standard DNS query; there simply is no (reverse) DNS entry for 198.154.218.168 and the code above fails.
As an example of the difference between the forward and reverse DNS lookups, when I was first allocated my static IP address, my provider (Comcast) mapped the IP address to some generic name like 75-148-###-###-Houston.hfc.comcastbusiness.net (random IP address example) and I had ask them to modify the reverse DNS entry to map to the name of my domain instead so that a forward and reverse lookup of the IP address and domain name matched. They maintained the lookup of the IP address on their DNS servers, and I maintained the lookup of the domain name on my DNS servers.
The page at yougetsignal.com must have been doing DNS (forward) lookups of names and storing them in the huge database available for purchase, which allows the web page to find all the names with the same IP. But there is no easy way to do that for an arbitrary IP address by querying DNS servers, unless you have considerable additional information or have already done the millions of lookups like that site.
I found that there are two websites providing this functionality. However I don't know how they implement that functionality.
http://reverseip.domaintools.com/
http://ipaddress.com/reverse_ip/
update:
domaintools provide api calls. maybe you can call domaintools api to get multiple domain names for the single IP.
Related
I have a list of computer names and I have to create a list of corresponding IP addresses. Which is done in a way like that:
Iterator<String> iter = listWithComputerNames.iterator();
while (iter.hasNext()) {
String curName = iter.next();
String ipAdress = "";
try {
InetAddress address = InetAddress.getByName(curName );
ipAdress = address.getHostAddress();
}
}
This works if I run the programm in the same domain as all the other computers. However, I would like to run in it as a batch job on a server, which is in a different domain (but still the same local network!).
Since both domains are within one company and computers can communicate with each other using IPs, I wonder whether there is a possibility to explicitly tell that my computer name is from a different domain: something like name.domain.local, such that it can be resolved to an IP even from a different domain. Does anyone know the trick?
InetAddress giriAddress = java.net.InetAddress.getByName("com.named.domain");
Then, if you want the IP as a String
String address = giriAddress.getHostAddress();
you can use this in your local env to update some kind of a database that the server then can access if he wants the IP address, or even better expose it as a web service
For an app I'm writing I want to completely avoid DNS host name resolution: I'm using numeric IPv4 and IPv6 addresses and I have no use for lookups. I've googled a bit but I can't find a class that allows me to open a connection without causing a DNS request to occur. Any hints?
TIA
If you invoke InetAddress.getByName() with a name that's not really a name, but is an IP address, Java won't perform the DNS lookup.
Another option is to use InetAddress.getByAddress() like so:
byte[] numericAddress = new byte[]{127,0,0,1};
InetAddress.getByAddress(numericAddress); //Will return an InetAddress that points to 127.0.0.1
Socket clientsocket = serverSock.accept();
String clientIP = clientsocket.getInetAddress().getHostAddress();
String clientName = clientsocket.getInetAddress().getHostName();
But clientIP and clientName both returns same value (like 192.168.173.46).
According to the documentation on the Oracle website I assume that you have a bad DNS setup. The following documentation is for the getHostName() method:
public String getHostName() Gets the host name for this IP address. If
this InetAddress was created with a host name, this host name will be
remembered and returned; otherwise, a reverse name lookup will be
performed and the result will be returned based on the system
configured name lookup service. If a lookup of the name service is
required, call getCanonicalHostName.
If there is a security manager, its checkConnect method is first
called with the hostname and -1 as its arguments to see if the
operation is allowed. If the operation is not allowed, it will return
the textual representation of the IP address.
Returns: the host name for this IP address, or if the operation is not
allowed by the security check, the textual representation of the IP
address. See Also: getCanonicalHostName(),
SecurityManager.checkConnect(java.lang.String, int)
Source
I do not know how you could fix this, but you could try adding a hostname manually in your hosts file.
I am trying to convert ip addresses to hostnames. I tried the answer given here. While the linux command "host" works for all the ip addresess I have, this code only works for some of them. Why would this be?
InetAddress addr = InetAddress.getByName("192.168.190.62");
String host = addr.getHostName();
System.out.println(host);
when looking at http://download.java.net/jdk7/archive/b123/docs/api/index.html?java/net/InetAddress.html you find...
getHostName
Gets the host name for this IP address.
If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName().
For example if I have the following SRV record defined in my DNS config
_dev._tcp IN SRV 0 0 8400 dev.server.com.
I can execute the following command
host -lt SRV server.com
And it gives me complete list of SRV records in the dns server(server.com). If I want to do the same thing using JNDI lookup
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attributes = ctx.getAttributes("server.com", new String [] { "SRV" });
return attributes;
The above code is not returning any attributes. If I change the penultimate line in the above code to this,
Attributes attributes = ctx.getAttributes("_dev._tcp.server.com", new String [] { "SRV" });
it works.
But the problem is I don't know the complete domain name in prior and I have to lookup the SRV record to find the complete domain name.
Any ideas as how to do this?
The -l parameter to /usr/bin/host instructs a DNS zone transfer. Unlike a conventional DNS query, zone transfers work over TCP.
Here's what host -lt SRV server.com does:
Finds out the name servers of server.com.
Connects to the first listed name server's port 53 with TCP.
Initiates a zone transfer.
Filters out the results in accordance to its -t srv filter.
You need to initiate a zone transfer via JNDI and filter out the results for what you are looking for. The method to invoke a DNS zone transfer in DnsContext is list().
NamingEnumeration<NameClassPair> names = ctx.list("server.com");
IMHO, JNDI has a terrible interface. It is designed to be very generic to be all naming and directory services and both mechanical and technical mismatch between the interface and DNS is simply frustrating.
If you are looking for a more pragmatic, easier to use and full features DNS library, have a look at dnsjava project at http://www.dnsjava.org
Also keep in mind not every DNS server allows zone transfers from untrusted hosts. Even LAN DNS servers won't allow zone transfers by default nowadays.