user121196
user121196

Reputation: 31060

querying against a particular dns server in java

I'm trying to query again a particular dns server both in linux shell using digg and using Java. The dig command works. but the java way doesn't. what's wrong?

 dig @dns.nic.it test.it


;; QUESTION SECTION:
;test.it.           IN  A

;; AUTHORITY SECTION:
test.it.        10800   IN  NS  dns2.technorail.com.
test.it.        10800   IN  NS  dns.technorail.com.
test.it.        10800   IN  NS  dns3.arubadns.net.
test.it.        10800   IN  NS  dns4.arubadns.cz.


java way

        public static void rootDNSLookup(String domainName) throws NamingException{

            String server="dns://dns.nic.it";

            Hashtable env = new Hashtable();
            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
            env.put("java.naming.provider.url",    server);

            DirContext ictx = new InitialDirContext(env);
            Attributes attrs = ictx.getAttributes(domainName, new String[] {"A", "AAAA", "NS", "SRV"});
            System.out.println(attrs);
            NamingEnumeration e = attrs.getAll();
            while(e.hasMoreElements()) {
                Attribute a = e.next();
                System.out.println(a.getID() + " = " + a.get());
            }

        }

java prints:

No attributes

Upvotes: 4

Views: 3966

Answers (2)

Stephen Coyle
Stephen Coyle

Reputation: 111

Don't try and get all attributes at the same time.

Use:

Attributes attrs = ictx.getAttributes(domainName, new String[] {"A"});

Use a String array that contains all attributes you want and iterate over the call passing in the attribute.

String[] attributes = {"A", "AAAA", "NS", "SRV"};
for (String attribute : attributes) {
    ...
    Attributes attrs = ictx.getAttributes(domainName, new String[] {attribute});
    ...
}

Upvotes: 0

Malvido
Malvido

Reputation: 29

The best way to go is to use the library dnsjava.

The particular class you are looking for is SimpleResolver. It has two constructors. When used with no parameters it will use the system's DNS settings. If you provide an IP address it will force that as the DNS server. You can find a great example of usage here: dnsjava Examples.

Upvotes: 2

Related Questions