Friday, 22 April 2016

How to do a DNS lookup using Java ?


DNS Lookup using a DNS server - using Linux

$ dig mediacenter.hello.com @10.117.56.15 

Output 
<<>> DiG 9.7.3-P3-RedHat-9.7.3-8.P3.el6_2.2 <<>> mediacenter.hello.com @10.117.56.15
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 18890
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;mediacenter.hello.com.    IN      A
;; ANSWER SECTION:
mediacenter.hello.com. 86400 IN    A       160.105.227.90
;; Query time: 33 msec
;; SERVER: 10.117.56.15#53(10.117.56.15)
;; WHEN: Wed Jan 20 08:33:30 2016
;; MSG SIZE  rcvd: 60
 


Java Library for DNS lookup


Java code
SimpleResolver resolver = new SimpleResolver("10.117.56.15");

Lookup lookup = new Lookup("mediacenter.hello.com", Type.A);
lookup.setResolver(resolver);

Record[] run = lookup.run();

System.out.println(Arrays.toString(run));

Output

[mediacenter.hello.com    86400   IN      A       160.105.227.90]

No comments:

Post a Comment

Note: only a member of this blog may post a comment.