Addev
Addev

Reputation: 32221

Check the dns lookup of a nameserver in Java

Given a domain and a nameserver ip I'd like to know where is that nameserver resolving the IP in java, how can I achieve it? Thanks

Upvotes: 2

Views: 4265

Answers (2)

jarnbjo
jarnbjo

Reputation: 34313

You have at least two options:

If your code has to run on any VM, you must use one of the many available Java DNS libraries. Googling for "java dns library" will give you many options.

If your code is only going to run on a Sun/Oracle VM, you can use the proprietary JNDI DNS provider like this:

Hashtable<String, Object> env = new Hashtable<String, Object>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url",    "dns://<your DNS server>");

DirContext ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes("www.heise.de", new String[] {"A", "AAAA"});

NamingEnumeration<? extends Attribute> e = attrs.getAll();
while(e.hasMoreElements()) {
    Attribute a = e.next();
    System.out.println(a.getID() + " = " + a.get());
}

This example will query the specified DNS server for all A and AAAA records for the host www.heise.de:

A = 193.99.144.85

AAAA = 2a02:2e0:3fe:100::7

Upvotes: 6

highlycaffeinated
highlycaffeinated

Reputation: 19867

If you need to query a specific nameserver to see how it is responding you can use the JNDI interface.

Upvotes: 3

Related Questions