Ruby Productivity Scripts : Simple Whois Client

Objective


To write a simple whois client using Ruby socket library.

Discussion


As web developers we have thousands of ideas for web applications than can make us rich quick. We buy domains in bulk when we get too excited. Why do we need to Google for the details? Why do we put up with Captchas? Why not write our own scripts to get the details from a terminal?

What is whois?

man whois 

shows you:

whois -- Internet domain name and network number directory service. 

The clients make a request to whois servers such as whois.internic.net to get details about a domain.

Steps


Step 1

Go to a terminal and run:

whois com

You will see:

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.

Aborting search 50 records found .....
COM---------------------------------------------------------WWW.COM
COM--------------------CN.COM
COM------COM.COM

BLAH, BLAH, BLAH...

Let's get some details about the next hotshot:

whois docker.com

The terminal utility speaks the whois protocol.

Step 2

Let's learn how to talk like a whois client.

$telnet whois.internic.net 43

You will see:

Trying 199.7.50.74...
Connected to whois.internic.net.
Escape character is '^]'.
rubyplus.com[Hit Enter here]

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered
with many different competing registrars. Go to http://www.internic.net
for detailed information.

   Domain Name: RUBYPLUS.COM
   Registrar: ENOM, INC.
   Sponsoring Registrar IANA ID: 48
   Whois Server: whois.enom.com
   Referral URL: http://www.enom.com
   Name Server: NS1.LINODE.COM
   Name Server: NS2.LINODE.COM
   Name Server: NS3.LINODE.COM
   Name Server: NS4.LINODE.COM
   Name Server: NS5.LINODE.COM
   Status: clientTransferProhibited http://www.icann.org/epp#clientTransferProhibited
   Updated Date: 02-feb-2015
   Creation Date: 12-feb-2007
   Expiration Date: 12-feb-2021

>>> Last update of whois database: Wed, 24 Jun 2015 00:07:00 GMT <<<

NOTICE: The expiration date displayed in this record is the date the 
blah, blah, blah...
Connection closed by foreign host.

We connect to a whois server on port 43, enter a domain, and hit return. The server sent us response packets to us and then disconnected the connection.

Step 3

We can now describe our program at a high level.

####High Level Description

  1. Create a TCP client connection to a Whois server such as whois.internic.net on port 43.
  2. Send the domain followed by a return character as the request.
  3. Read the response from the server.
  4. Print the response to standard output.

Step 4

Here is the code.

require 'socket'

host = 'whois.internic.net'         # The whois server
port = 43                           # Default whois port

# This is the whois request we send to get domain details
request = "rubyflow.com\r\n\r\n"

socket = TCPSocket.open(host,port)  # Connect to server
socket.print(request)               # Send request
response = socket.read              # Read complete response

print response                      # And display it

The Process


Reverse engineer to find out how something is implemented by reading code. The ruby gems that has evolved over a period of years is not readable because the 'why' is hidden. It is explained as a blog post with diagrams on the whois ruby gem author's blog.

It is easier to read similar programs in other languages such as PHP, C# and Python. Especially if it is small and is focused on doing what you want to accomplish.

After reading the code, create a high-level description of what the program should do. Write down all the steps. For each step, read Ruby docs, figure out the needed code to implement the desired functionality.

Summary


In this article, we wrote a simple whois client to retrieve details about a domain. We learned how to use Ruby during this process. You can send query to get more details by reading the whois protocol specification. I found it difficult to understand the existing code of whois gem. It is easier to read PHP example with just one file containing 400 lines of code to get a sense of how to write a whois client. Do not be afraid of reading source code of examples from languages such as PHP, C# and Python. You will learn a lot and it can lead to cross pollination of ideas, enabling you to build better gems.

References


  1. Simple C# Class to Get Whois Information
  2. Ruby Docs
  3. Whois Protocol
  4. Python Whois Client
  5. How does whois work
  6. An intelligent pure Ruby WHOIS client and parser
  7. Whois Client in PHP


Related Articles


Create your own user feedback survey