Whois Lookup



There are numerous sample codes floating around the web that show how to do a WikiPedia:Whois lookup, but none seem to get it quite right. So here's code that works for all TLDs, and also smoothes out some of the peculiarities of various Whois servers. It uses the Apache Commons Net library (which by itself does not implement a comprehensive lookup solution).

The general approach is:

Additional difficulties to watch out for:


import java.util.*;

import org.apache.commons.net.whois.WhoisClient;
// You don't actually need the complete Apache Commons Net library;
// the FingerClient, SocketClient and WhoisClient classes are sufficient.

public class WhoisLookup {

    public static final String IANA_WHOIS_SERVER = "whois.iana.org";
    public static final int WHOIS_PORT = 43;

    public static void main (String[] args) throws Exception {
        String host = args[0];
        // here one could peruse the https://publicsuffix.org/list/
        // in order to determine the domain from the host

        String whoisServer = null;

        WhoisClient whoisClient = new WhoisClient();
        if (host.endsWith("web.za")) {
            whoisServer = "web-whois.registry.net.za";
        } else if (host.endsWith("org.za")) {
            whoisServer = "org-whois.registry.net.za";
        } else if (host.endsWith("net.za")) {
            whoisServer = "net-whois.registry.net.za";
        } else if (host.endsWith("co.za")) {
            whoisServer = "coza-whois.registry.net.za";
        } else {
            // get the address of the authoritative Whois server from IANA
            whoisClient.connect(IANA_WHOIS_SERVER, WHOIS_PORT);
            String results = whoisClient.query(host);
            whoisClient.disconnect();

            int idx = results.indexOf("whois:");
            if (idx < 0) {
                System.out.println("No WHOIS server available for "+host);
                System.exit(0);
                // TODO: if the TLD is a gTLD listed in https://github.com/rfc1036/whois/blob/next/new_gtlds_list
                // set whoisServer to "whois.nic.''TLD''
                // TODO: if the TLD is listed in https://github.com/rfc1036/whois/blob/next/tld_serv_list
                // set whoisServer to the one listed in there. That would obviate the need for the .za special cases
            } else {
                results = results.substring(idx+6).trim();
                whoisServer = results.substring(0, results.indexOf("\n"));
            }
        }

        String tld = host.substring(host.lastIndexOf(".")+1).trim().toLowerCase();

        String query = host;
        switch (tld) {
            case "com":
                query = "domain " + host;
                break;
            case "de":
                query = "-T dn " + host;
                break;
            case "dk":
                query = "--show-handles " + host;
                break;
            case "jp":
                query = host + "/e";
                break;
            default:
        }

        // get the actual Whois data
        whoisClient.connect(whoisServer, WHOIS_PORT);
        String results = whoisClient.query(query);
        whoisClient.disconnect();
        printResults(whoisServer, results);

        // if there is a more specific server, query that too
        int idx = results.toLowerCase().indexOf("whois server:");
        if (idx != -1) {
            results = results.substring(idx+13).trim();
            whoisServer = results.substring(0, results.indexOf("\n")).trim();
            whoisClient.connect(whoisServer, WHOIS_PORT);
            results = whoisClient.query(host);
            whoisClient.disconnect();
            printResults(whoisServer, results);
        }
    }

    private static void printResults (String server, String results) {
        // remove Windows-style line endings
        results = results.replaceAll("\r""");

        boolean seenDashes = false;
        boolean omit = false;
        String[] lines = results.split("\n");
        List<String> newLines = new ArrayList<>();
        for (String line : lines) {
            String trim = line.trim();
            switch (server) {
                case "whois.denic.de":
                case "whois.tcinet.ru":
                case "whois.nic.fr":
                    if (! trim.startsWith("%"))
                        newLines.add(line);
                    break;
                case "whois.dk-hostmaster.dk":
                case "whois.arin.net":
                    if (! trim.startsWith("#"))
                        newLines.add(line);
                    break;
                case "whois.nic.uk":
                    if (trim.startsWith("--"))
                        omit = true;
                    if (! omit)
                        newLines.add(line);
                    break;
                case "whois.jprs.jp":
                    if (! trim.startsWith("[ "))
                        newLines.add(line);
                    break;
                case "whois.educause.edu":
                    if (seenDashes)
                        newLines.add(line);
                    if (trim.startsWith("------"))
                        seenDashes = true;
                    break;
                default:
                    if (! omit)
                        newLines.add(line);
                    if (trim.startsWith(">>>"))
                        omit = true;
                    break;
            }
        }

        System.out.println("\n\nFrom "+server+":\n");
        for (String line : newLines) {
            System.out.println(line);
        }
    }
}


CodeSnippets