Created
March 6, 2017 08:41
-
-
Save hfs/31533e692022298d2ed5cc7f4f534698 to your computer and use it in GitHub Desktop.
Test DNS resolution via JNDI from the command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Hashtable; | |
import javax.naming.Context; | |
import javax.naming.NamingEnumeration; | |
import javax.naming.NamingException; | |
import javax.naming.directory.Attribute; | |
import javax.naming.directory.Attributes; | |
import javax.naming.directory.DirContext; | |
import javax.naming.directory.InitialDirContext; | |
public class JndiDnsTest { | |
public static void main(String[] args) { | |
if (args.length < 2) { | |
System.out.println("Usage: " + JndiDnsTest.class.getName() + | |
" name record-types..."); | |
return; | |
} | |
String name = args[0]; | |
String[] recordTypes = new String[args.length - 1]; | |
System.arraycopy(args, 1, recordTypes, 0, args.length - 1); | |
Hashtable<String, String> env = new Hashtable<String,String>(); | |
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); | |
try { | |
DirContext ctx = new InitialDirContext(env); | |
Attributes dnsQueryResult = ctx.getAttributes(name, recordTypes); | |
if (dnsQueryResult == null) { | |
System.out.println("Not found: '" + name + "'"); | |
} | |
for (String rrType: recordTypes) { | |
Attribute rr = dnsQueryResult.get(rrType); | |
if (rr != null) { | |
for (NamingEnumeration<?> vals = rr.getAll(); vals.hasMoreElements();) { | |
System.out.print(rrType + "\t"); | |
System.out.println(vals.nextElement()); | |
} | |
} | |
} | |
} catch (NamingException e) { | |
e.printStackTrace(System.err); | |
} | |
System.out.println("\nThe DNS search list:"); | |
for (Object entry: sun.net.dns.ResolverConfiguration.open().searchlist()) { | |
System.out.println(entry); | |
} | |
System.out.println("\nsun.net.spi.nameservice.domain = " + | |
System.getProperty("sun.net.spi.nameservice.domain")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment