DNS query in JAVA

zJK picture zJK · Feb 17, 2015 · Viewed 23.9k times · Source

I am messing around with DNS services in Java - I am specifically trying to lookup all google.com addresses and display them in an array, similar to running a lookup using nslookup:

nslookup -q=TXT _netblocks.google.com 8.8.8.8

I am using InetAddress for this but keep on getting exception errors. Since the errors refer to 'Unknown Host' I don't think InetAddress can read TXT records (if I use google.com it works, but that does't show the full IP Range). Below is my code:

InetAddress dnsresult[] = InetAddress.getAllByName("_netblocks.google.com");
            for (int i=0; i<dnsresult.length; i++)
            System.out.println (dnsresult[i]);

Would appreciate it if someone can point me in the right direction.

-JK

Answer

Manish Maheshwari picture Manish Maheshwari · Feb 17, 2015

You cannot lookup TXT or other DNS records InetAddress class. InetAddress.getAllByName() looks up for A, or AAAA records only.

Check DNS Java for your needs.