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?
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.
Go to a terminal and run:
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:
The terminal utility speaks the whois protocol.
Let's learn how to talk like a whois client.
$telnet whois.internic.net 43
You will see:
Trying 220.127.116.11... 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.
We can now describe our program at a high level.
####High Level Description
- Create a TCP client connection to a Whois server such as whois.internic.net on port 43.
- Send the domain followed by a return character as the request.
- Read the response from the server.
- Print the response to standard output.
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
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.
- Simple C# Class to Get Whois Information
- Ruby Docs
- Whois Protocol
- Python Whois Client
- How does whois work
- An intelligent pure Ruby WHOIS client and parser
- Whois Client in PHP
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