Java – Get nameservers of a website

DNS

In this tutorial, we will show you how to get the nameservers of a website using Java + dig command.

1. Using Dig Command

1.1 On Linux, we can use dig command to query a DNS lookup of a website, for example :


$ dig any mkyong.com

//...
mkyong.com.             299     IN      A       162.159.x.x
mkyong.com.             299     IN      A       198.41.x.x
mkyong.com.             299     IN      MX      10 aspmx2.googlemail.com.
mkyong.com.             299     IN      MX      10 aspmx3.googlemail.com.
mkyong.com.             299     IN      MX      1 aspmx.l.google.com.
mkyong.com.             299     IN      MX      5 alt1.aspmx.l.google.com.
mkyong.com.             299     IN      MX      5 alt2.aspmx.l.google.com.
mkyong.com.             21599   IN      NS      max.ns.cloudflare.com.
mkyong.com.             21599   IN      NS      erin.ns.cloudflare.com.

;; Query time: 246 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Mar 18 08:35:24 Malay Peninsula Standard Time 2014
;; MSG SIZE  rcvd: 387

P.S The dig is a built-in command on *nix, no need to install.

1.2 On Windows, you need to install the BIND package (a zip file) from isc.org, and the dig command is inside the zip file. After extracting the downloaded zip file, set the “bind” folder to the environment variable, so that you can use the dig command everywhere.


C:\> dig +short NS mkyong.com
max.ns.cloudflare.com.
erin.ns.cloudflare.com.

2. Java DNS Example

In Java, we can call the external dig command to get the name servers easily.

ShellCommand.java

package com.mkyong.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ShellCommand {

	public static void main(String args[]) {

		ShellCommand shell = new ShellCommand();

		String result = shell.run("dig +short NS mkyong.com");
		System.out.println(result);

	}

	public String run(String command) {

		StringBuilder sb = new StringBuilder();
		BufferedReader reader = null;
		Process p;

		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			reader = new BufferedReader(
				new InputStreamReader(p.getInputStream()));

			String line = "";
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return sb.toString();

	}

}

Output


max.ns.cloudflare.com.
erin.ns.cloudflare.com.

3. Get mail servers

In addition, you can use “MX” to get mail servers.


C:\> dig +short MX mkyong.com

10 aspmx2.googlemail.com.
5 alt2.aspmx.l.google.com.
5 alt1.aspmx.l.google.com.
1 aspmx.l.google.com.
10 aspmx3.googlemail.com.
Note
If you want more DNS features, try dnsjava library, an implementation of DNS in Java.

References

  1. 10 Linux DIG Command Examples for DNS Lookup
  2. Wikipedia : Dig (command)
  3. DNSJava official site
  4. How To Execute Shell Command From Java
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Namo
Guest
Namo

Can you give some examples of situations in which you’d want to use this?