2011-08-17 16 views
8

Ho bisogno di ottenere l'indirizzo DNS, ad esempio "http://stackoverflow.com/questions/ask". Ho usato il seguente codice e in grado di ottenere sotto forma di 192.X.X.X.È necessario eseguire una ricerca DNS inversa di un particolare indirizzo IP in java

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

Qual è la domanda? Ha funzionato o ti ha dato un errore? Qual è stato l'errore? – unholysampler

+0

@ unholysampler: Ho bisogno di ottenere l'indirizzo DNS in parole..come www.google.com. Questo codice fornisce sotto forma di sottorete. – Manikandan

risposta

7

CodingForums similar question - vedi post # 5 (NOTA BENE: codice non è mio, non testato da me)


Codice da origine collegata:

/** 
* 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

Grazie. Risposta migliore poiché è possibile definire i propri dns come: env.put (Context.PROVIDER_URL, "dns: //" + DNS_SERVER_IP) e molte altre opzioni per il contesto iniziale. – 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

Fornisco 216,239,51,99 nel byte []. Mi ha chiesto di digitare caste to byte per i primi due (216 e 239). Io scrivo casta. Stampa 216.239.51.99 solo non in parole. – Manikandan

+1

@Jack se disponi di un box linux/mac e usi dig -x 216.239.51.99 vedrai che non c'è risposta per quel numero. Ciò significa che non vi è alcuna voce in DNS per questo. Ecco perché ottieni il numero di risposta in risposta. – Clint

7

I ho preso il codice collegato a @Sam DeHaan, l'ho pulito un po 'e l'ho testato.

/** 
* 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

È importante notare che non è sempre possibile eseguire una ricerca inversa perché i dati non sono sempre presenti in DNS. –

Problemi correlati