Reputation: 151
I am messing around with DNS services in Java. I am specifically trying to look up 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 I keep on getting exceptions. Since the exceptions refer to an 'Unknown Host', I don't think InetAddress
can read TXT records (if I use google.com it works, but that doesn'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]);
I would appreciate it if someone could point me in the right direction.
Upvotes: 15
Views: 37787
Reputation: 20263
I ended up using Google's "JSON API for DNS over HTTPS (DoH)"
https://developers.google.com/speed/public-dns/docs/doh/json
Upvotes: 1
Reputation: 1888
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.
Upvotes: 7
Reputation: 311054
InetAddress
doesn't do this, but you can accomplish DNS TXT record lookups in Java via the JNDI DNS provider.
Upvotes: 5
Reputation: 4134
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.
Upvotes: 12