Skip to content

Instantly share code, notes, and snippets.

@JurrianFahner
Created October 9, 2015 12:47
Show Gist options
  • Save JurrianFahner/0226333cf2cf95847961 to your computer and use it in GitHub Desktop.
Save JurrianFahner/0226333cf2cf95847961 to your computer and use it in GitHub Desktop.
A java class to find the specifics of a given hostname
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* DNSLookup is a class which demonstrates several features of <a href="http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html">java.net.InetAddress</a>
* @author Jurrian Fahner
*/
public class DNSLookup {
/**
* Prints the details of a given hostname
*/
public static void printDetails(String hostname) {
try {
InetAddress[] inetAddress = InetAddress.getAllByName(hostname);
for (InetAddress item:inetAddress) {
System.out.println("Which Host:" + whichHost);
System.out.println("Canonical Host Name:" + inetAddress.getCanonicalHostName());
System.out.println("Host Name:" + inetAddress.getHostName());
System.out.println("Host Address:" + inetAddress.getHostAddress());
System.out.println("Loopback Address: " + inetAddress.getLoopbackAddress());
}
} catch (UnknownHostException ex) {
Logger.getLogger(DNSLookup.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("No hostname supplied...");
System.out.println("Usage: DNSLookup HOSTNAME");
} else {
printDetails(args[0]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment