Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to perform a reverse DNS lookup of a particular IP address in java

Tags:

java

dns

I need to get the DNS address, for example "http://stackoverflow.com/questions/ask". I used the following code and able to get in the form of 192.X.X.X.

  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
  DirContext ictx = new InitialDirContext(env);
  String dnsServers = (String) ictx.getEnvironment().get("java.naming.provider.url");

  System.out.println("DNS Servers: " + dnsServers ); 
like image 288
Manikandan Avatar asked Aug 17 '11 18:08

Manikandan


People also ask

What is reverse DNS of the IP?

A reverse DNS lookup is a DNS query for the domain name associated with a given IP address. This accomplishes the opposite of the more commonly used forward DNS lookup, in which the DNS system is queried to return an IP address.

Why do we need reverse DNS lookup?

Why is this so important? Reverse DNS is mainly used to track the origin of a website visitor, the origin of an e-mail message, etc. It is usually not as critical as the classic DNS, visitors will reach the website even without the presence of reverse DNS for the IP of the web server or the IP of the visitor.


2 Answers

InetAddress ia = InetAddress.getByAddress(new byte[] {74,125,127,106});
// or 
ia = InetAddress.getByName("74.125.127.106");
System.out.println(ia.getCanonicalHostName());
like image 131
Clint Avatar answered Sep 24 '22 00:09

Clint


I have taken the code linked to by @Sam DeHaan, cleaned it up a bit and tested it.

/**
 * Do a reverse DNS lookup to find the host name associated with an IP address. Gets results more often than
 * {@link java.net.InetAddress#getCanonicalHostName()}, but also tries the Inet implementation if reverse DNS does
 * not work.
 * 
 * Based on code found at http://www.codingforums.com/showpost.php?p=892349&postcount=5
 * 
 * @param ip The IP address to look up
 * @return   The host name, if one could be found, or the IP address
 */
private static String getHostName(final String ip)
 {
   String retVal = null;
   final String[] bytes = ip.split("\\.");
   if (bytes.length == 4)
   {
      try
      {
         final java.util.Hashtable<String, String> env = new java.util.Hashtable<String, String>();
         env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
         final javax.naming.directory.DirContext ctx = new javax.naming.directory.InitialDirContext(env);
         final String reverseDnsDomain = bytes[3] + "." + bytes[2] + "." + bytes[1] + "." + bytes[0] + ".in-addr.arpa";
         final javax.naming.directory.Attributes attrs = ctx.getAttributes(reverseDnsDomain, new String[]
         {
            "PTR",
         });
         for (final javax.naming.NamingEnumeration<? extends javax.naming.directory.Attribute> ae = attrs.getAll(); ae.hasMoreElements();)
         {
            final javax.naming.directory.Attribute attr = ae.next();
            final String attrId = attr.getID();
            for (final java.util.Enumeration<?> vals = attr.getAll(); vals.hasMoreElements();)
            {
               String value = vals.nextElement().toString();
               // System.out.println(attrId + ": " + value);

               if ("PTR".equals(attrId))
               {
                  final int len = value.length();
                  if (value.charAt(len - 1) == '.')
                  {
                     // Strip out trailing period
                     value = value.substring(0, len - 1);
                  }
                  retVal = value;
               }
            }
         }
         ctx.close();
      }
      catch (final javax.naming.NamingException e)
      {
         // No reverse DNS that we could find, try with InetAddress
         System.out.print(""); // NO-OP
      }
   }

   if (null == retVal)
   {
      try
      {
         retVal = java.net.InetAddress.getByName(ip).getCanonicalHostName();
      }
      catch (final java.net.UnknownHostException e1)
      {
         retVal = ip;
      }
   }

   return retVal;
 }
like image 27
Konstantin Tarashchanskiy Avatar answered Sep 26 '22 00:09

Konstantin Tarashchanskiy