4

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
user121196
  • 30,032
  • 57
  • 148
  • 198

2 Answers2

2

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.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Malvido
  • 29
  • 1
  • 1
  • 4
  • 1
    Link is no longer valid. I am trying to do some simple dig emulation using only java (no BIND installed on the requester). Any chance to get a clue, please? :) – DraxDomax Mar 10 '20 at 18:02
0

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});
    ...
}
Stephen Coyle
  • 111
  • 1
  • 5