2011-08-17 5 views
8

में किसी विशेष आईपी पते के रिवर्स डीएनएस लुकअप को करने की आवश्यकता है, मुझे DNS पता प्राप्त करने की आवश्यकता है, उदाहरण के लिए "http://stackoverflow.com/questions/ask"। मैंने निम्नलिखित कोड का उपयोग किया और 1 9 2.एक्स.एक्स.एक्स के रूप में प्राप्त करने में सक्षम।जावा

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); 
+0

प्रश्न क्या है? क्या यह काम करता है या यह आपको एक त्रुटि देता है? त्रुटि क्या थी? – unholysampler

+0

@ unholysampler: मैं words..like www.google.com में dns पता पाने के लिए की जरूरत है। यह कोड सबनेट के रूप में देता है। – Manikandan

उत्तर

7

CodingForums similar question - देखते हैं कि पोस्ट # 5 (अस्वीकरण: कोड मेरा नहीं, नहीं मेरे द्वारा परीक्षण किया है):

/** 
* 25 Nov 2009 REVERSE DNS LOOKUP USING JNDI 
* In this example the IP being looked up is 211.21.152.4 
* The octets are reversed (4.152.21.211) 
* and appended to the in-addr.arpa zone: 
* 4.152.21.211.in-addr.arpa 
*/ 

import javax.naming.*; 
import javax.naming.directory.*; 
import java.util.*; 

public class dns { 
    public static void main(String[] args) { 
     try { 
      Hashtable env = new Hashtable(); 
      env.put("java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory"); 

      DirContext ctx = new InitialDirContext(env); 
      Attributes attrs = ctx.getAttributes("4.152.21.211.in-addr.arpa",new String[] {"PTR"}); 

      for (NamingEnumeration ae = attrs.getAll();ae.hasMoreElements();) { 
       Attribute attr = (Attribute)ae.next(); 
       String attrId = attr.getID(); 
       for (Enumeration vals = attr.getAll();vals.hasMoreElements(); 
       System.out.println(attrId + ": " + vals.nextElement())); 
      } 

      ctx.close(); 
     } 

     catch(Exception e) { 
      System.out.println("NO REVERSE DNS"); 
     } 
    } 
} 
+1

धन्यवाद। सर्वश्रेष्ठ उत्तर के बाद से आप के रूप में अपने स्वयं के DNS परिभाषित कर सकते हैं: env.put (Context.PROVIDER_URL, "DNS: //" + DNS_SERVER_IP) और प्रारंभिक संदर्भ के लिए कई अन्य विकल्प। – dalvarezmartinez1

13
InetAddress ia = InetAddress.getByAddress(new byte[] {74,125,127,106}); 
// or 
ia = InetAddress.getByName("74.125.127.106"); 
System.out.println(ia.getCanonicalHostName()); 
+0

मैं बाइट [] में 216,239,51,99 दे। यह मेरे पहले दो (216 और 239) के लिए बाइट के लिए जाति लिखने के लिए कहा। मैं जाति टाइप करता हूँ। यह केवल 216.239.51.99 प्रिंट करता है जो शब्दों में नहीं है। – Manikandan

+1

@ जैक यदि आपके पास लिनक्स/मैक बॉक्स है और dig -x 216.239.51.99 का उपयोग करें तो आप देखेंगे कि उस नंबर का कोई जवाब नहीं है। इसका मतलब है कि इसके लिए DNS में कोई प्रविष्टि नहीं है। यही कारण है कि आप जवाब में वापस नंबर मिलता है। – Clint

7

मैं


जुड़ा हुआ स्रोत से कोड कोड @Sam DeHaan से जुड़े ले लिया है, यह थोड़ा साफ है और यह परीक्षण किया गया।

/** 
* 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; 
} 
+1

यह ध्यान रखना महत्वपूर्ण है कि रिवर्स लुकअप करना हमेशा संभव नहीं होता है क्योंकि डेटा हमेशा DNS में नहीं होता है। –