As per definition the dig command
in Linux is primarily used to query dns servers
. It is useful tool for network troubleshooting. It is popular due to its flexibility
and very clear output
over host command.
In this tutorial we will go over what all commands you could execute to get DNS records as per your need. Best way to understand Dig Command.
Another must read
Explore WhoisClient library
Let’s get started:
- Create java class CrunchifyZoneRecordFinder.java
- Create method crunchifyExecuteCommand(String cmd) which executes and prints result of linux command
- In this Java tutorial we will get below 4 results
- Domain A records (IPs)
- TXT records
- MX records (email)
- NS records (Name servers)
package com.crunchify.tutorials; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author https://crunchify.com * */ public class CrunchifyZoneRecordFinder { public static void main(String args[]) { CrunchifyZoneRecordFinder crunchifyDigCmd = new CrunchifyZoneRecordFinder(); crunchifyDigCmd.crunchifyExecuteCommand("dig any crunchify.com"); crunchifyDigCmd.crunchifyExecuteCommand("dig +short crunchify.com MX"); crunchifyDigCmd.crunchifyExecuteCommand("dig +short crunchify.com NS"); crunchifyDigCmd.crunchifyExecuteCommand("dig +short crunchify.com A"); crunchifyDigCmd.crunchifyExecuteCommand("dig +short crunchify.com TXT"); } public void crunchifyExecuteCommand(String cmd) { String s = null; System.out.println("\n=========== Executing command: " + cmd + " ==========="); try { Process p = Runtime.getRuntime().exec(cmd); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); // read the output from the command while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command while ((s = stdError.readLine()) != null) { System.out.println(s); } } catch (IOException e) { System.out.println("exception happened - here's what I know: "); e.printStackTrace(); } } }
Output/ Result:
=========== Executing command: dig any crunchify.com =========== ; <<>> DiG 9.8.3-P1 <<>> any crunchify.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29481 ;; flags: qr rd ra; QUERY: 1, ANSWER: 11, AUTHORITY: 0, ADDITIONAL: 11 ;; QUESTION SECTION: ;crunchify.com. IN ANY ;; ANSWER SECTION: crunchify.com. 6492 IN A 45.56.77.128 crunchify.com. 34789 IN NS ns3.squidix.net. crunchify.com. 34789 IN NS ns1.squidix.net. crunchify.com. 34789 IN NS ns2.squidix.net. crunchify.com. 34789 IN NS ns4.squidix.net. crunchify.com. 13107 IN MX 10 alt4.aspmx.l.google.com. crunchify.com. 13107 IN MX 1 aspmx.l.google.com. crunchify.com. 13107 IN MX 5 alt2.aspmx.l.google.com. crunchify.com. 13107 IN MX 10 alt3.aspmx.l.google.com. crunchify.com. 13107 IN MX 5 alt1.aspmx.l.google.com. crunchify.com. 13675 IN TXT "v=spf1 include:_spf.google.com ~all" ;; ADDITIONAL SECTION: ns3.squidix.net. 52363 IN A 168.235.80.31 ns1.squidix.net. 52363 IN A 148.163.67.8 ns2.squidix.net. 52363 IN A 107.191.102.190 ns4.squidix.net. 52363 IN A 148.163.67.8 alt4.aspmx.l.google.com. 180 IN A 74.125.141.27 aspmx.l.google.com. 119 IN A 74.125.28.26 aspmx.l.google.com. 119 IN AAAA 2607:f8b0:400e:c04::1a alt2.aspmx.l.google.com. 119 IN A 74.125.196.26 alt2.aspmx.l.google.com. 119 IN AAAA 2607:f8b0:4002:c03::1b alt3.aspmx.l.google.com. 180 IN A 173.194.204.27 alt1.aspmx.l.google.com. 119 IN A 74.125.142.26 ;; Query time: 80 msec ;; SERVER: 10.249.156.21#53(10.249.156.21) ;; WHEN: Fri Oct 2 12:46:19 2015 ;; MSG SIZE rcvd: 493 =========== Executing command: dig +short crunchify.com MX =========== 10 alt4.aspmx.l.google.com. 1 aspmx.l.google.com. 5 alt2.aspmx.l.google.com. 10 alt3.aspmx.l.google.com. 5 alt1.aspmx.l.google.com. =========== Executing command: dig +short crunchify.com NS =========== ns3.squidix.net. ns1.squidix.net. ns2.squidix.net. ns4.squidix.net. =========== Executing command: dig +short crunchify.com A =========== 45.56.77.128 =========== Executing command: dig +short crunchify.com TXT =========== "v=spf1 include:_spf.google.com ~all"
If you want to run above program on Windows environment then you could follow the tutorial to get windows command. I’ve executed this program on my Macbook Pro.