How do I query a Whois server using a socket from names in a text file?
Author: Deron Eriksson
Description: This Java tutorial describes how to query a Whois server using domain names in a text file.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
The MultiWhois class shown here uses bits and pieces of other tutorials to read a text file of domain names, perform WhoisW queries on those domain names, and output the results to an HTMLW file. This class uses the whois.enom.com Whois server. Whois servers can send back results in many different formats, so the code that determines if a domain name 'Exists' or 'Does Not Exist' is not robust. This code is not robust in many other ways too. For example, if the server returns a 'Malformed Request' message, the MultiWhois class reports that the name exists even though this is an error condition. However, MultiWhois does serve to illustrate some useful concepts. This code should not be used to bombard a Whois server with an unreasonable number of requests in a short period of time. When querying a list of about 300 domain names, I received a message after about 150 saying that I'd need to wait 30 minutes before resuming, since I'd exceeded a query quota. In addition, in order to try to be nice, I added a Thread.sleep(3000) after each query in order to throttle the requests to the Whois server. The MultiWhois class reads a list of domain names in from the "input.txt" file (one name per line) into a List. It loops over this list of domain names, performing a Whois query for each name via a socket connection to the Whois server. It generates output in HTML tabular format and writes this to the "results.htm" file. MultiWhois.javapackage test; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class MultiWhois { public static final String WHOIS_SERVER = "whois.enom.com"; public static void main(String[] args) throws Exception { String inputFile = "input.txt"; String outputFile = "results.htm"; FileWriter fileWriter = new FileWriter(outputFile); PrintWriter out = new PrintWriter(fileWriter); out.println("<table border=1 cellpadding=2 cellspacing=0>"); out.println("<tr><th>Name</th><th>Status</th><th>output</th></tr>"); List<String> nameList = getNamesFromFile(inputFile); for (String domainNameToCheck : nameList) { performQueryAndOutputResultToWriter(out, domainNameToCheck); Thread.sleep(3000); // let's put in a pause so we don't bombard the whois server } out.println("</table>"); out.flush(); out.close(); fileWriter.close(); } public static String performWhoisQuery(String host, int port, String query) throws Exception { System.out.println("**** Performing whois query for '" + query + "' at " + host + ":" + port); Socket socket = new Socket(host, port); InputStreamReader isr = new InputStreamReader(socket.getInputStream()); BufferedReader in = new BufferedReader(isr); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println(query); String line = ""; StringBuffer sb = new StringBuffer(); while ((line = in.readLine()) != null) { System.out.println(line); sb.append(line); sb.append("<br/>\n"); } return sb.toString(); } public static void performQueryAndOutputResultToWriter(PrintWriter out, String domainNameToCheck) throws Exception { String queryResult = performWhoisQuery(WHOIS_SERVER, 43, domainNameToCheck); boolean found = true; if (queryResult.indexOf("FW-1") != -1) { found = false; } out.println("<tr><td>"); out.println(domainNameToCheck); out.println("</td><td>"); if (found) { out.println("Exists"); } else { out.println("Does Not Exist"); } out.println("</td><td>"); out.println(queryResult); out.println("</td></tr>"); } public static List<String> getNamesFromFile(String inputFile) throws Exception { FileReader fileReader = new FileReader(inputFile); BufferedReader bufferedReader = new BufferedReader(fileReader); String inputLine; List<String> lineList = new ArrayList<String>(); while ((inputLine = bufferedReader.readLine()) != null) { lineList.add(inputLine); } fileReader.close(); return lineList; } } I tested MultiWhois with the following input file: input.txtimaginarydomainname.com abcnews.com 54321squirrel.com Executing MultiWhois resulted in the generation of the following results.htm file given the input file listed above. results.htm
|