How to do Reverse DNS (rDNS) lookup in java

Java

What is Reverse DNS Lookup

Reverse DNS lookup or reverse DNS resolution (rDNS) is the determination of a domain name that is associated with a given IP address using the Domain Name System (DNS) of the Internet. Computer networks use the Domain Name System to determine the IP address associated with a domain name. This process is also known as forward DNS resolution. Reverse DNS lookup is the inverse process, the resolution of an IP address to its designated domain name.

The reverse DNS database of the Internet is rooted in the Address and Routing Parameter Area (arpa) top-level domain of the Internet. IPv4 uses the in-addr.arpa domain and the ip6.arpa domain is delegated for IPv6.

Here in the example we will use the host command of *nix, which takes IP address as arguments and provides the rDNS against it.

For how to execute OS/Shell command please click here.

package com.jkoder.java.os;

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

public class ReverseDNS {

	public static void main(String[] args){
		try {
			String output = executeShellCommand("host", "192.185.144.90");
			System.out.println(output);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static String executeShellCommand(String command, String arg) throws IOException{
		StringBuffer output = new StringBuffer();
		
		//Create ProcessBuilder instance
        java.lang.ProcessBuilder processBuilder = new java.lang.ProcessBuilder(command, arg);
        
        final Process process = processBuilder.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
        	output.append(line + "\n");
        }
        return output.toString();
	}
}

Output of the above program

90.144.185.192.in-addr.arpa domain name pointer 192-185-144-90.unifiedlayer.com.