Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DNS query in JAVA

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

like image 640
zJK Avatar asked Feb 17 '15 10:02

zJK


3 Answers

Here is an example that does what you are trying to do:

Attribute attr = new InitialDirContext().getAttributes("dns:_netblocks.google.com", new String[] {"TXT"}).get("TXT");
System.out.println("attr.get() = " + attr.get());
System.out.println("attr.getAll() = " + Collections.list(attr.getAll()));

If you want to use a custom dns server use "dns://1.1/_netblocks.google.com" instead.

like image 100
user988346 Avatar answered Oct 26 '22 00:10

user988346


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.

like image 9
Manish Maheshwari Avatar answered Oct 25 '22 23:10

Manish Maheshwari


InetAddress doesn't do this, but you can accomplish DNS TXT record lookups in Java via the JNDI DNS provider.

like image 5
user207421 Avatar answered Oct 26 '22 01:10

user207421