4
votes

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
1
This code does not print 'no attributes'.user207421
did it print anything for you?user121196
There is nothing in this code that prints 'no attributes'. Please explain, or amend your question.user207421
System.out.println(attrs); prints "no attributes"..........user121196

1 Answers

3
votes

I know it has been a long time, but for the rest of us that kept searching for an answer, 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: https://github.com/dnsjava/dnsjava/blob/master/dig.java