Ruby Productivity Scripts : Simple Whois Client


To write a simple whois client using Ruby socket library.


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 to get details about a domain.


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
for detailed information.

Aborting search 50 records found .....


Let's get some details about the next hotshot:


The terminal utility speaks the whois protocol.

Step 2

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

$telnet 43

You will see:

Connected to
Escape character is '^]'.[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
for detailed information.

   Domain Name: RUBYPLUS.COM
   Registrar: ENOM, INC.
   Sponsoring Registrar IANA ID: 48
   Whois Server:
   Referral URL:
   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
   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 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 = ''         # The whois server
port = 43                           # Default whois port

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

socket =,port)  # Connect to server
socket.print(request)               # Send request
response =              # 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.


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.


  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

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.