Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create a one-step whois lookup script Internet
OS X comes with the 'whois' command, a useful utility for checking domain name registrations and finding domain contacts. It's run from Terminal.app. However, since the advent of all the new ICANN approved registrars for domains, one must run 'whois' twice to actually get some meaningful results if the domain is not registered at Network Solutions. Run whois once to get the whois server that's serving the domain information, and a second time (with the "-h" swtich), to actually query the first referenced whois server.

I've written a little bash script to make this just a little bit easier (and in one step). It's included below in the hopes others may find it useful.
 #!/bin/bash
echo "Getting server..."
export SERVER=`whois $1 | grep -i whois\ server: |
awk '{print $3'}`
echo "Searching server: $SERVER"
whois -h $SERVER $1
[IMPORTAND NOTE: The "export" line must be entered as one line, not two; just replace the line break with a space...]

Copy the above text to your clipboard (Command+C), then fire off Terminal.app to create the script. Start a pico session, as root, and create the script with sudo pico /usr/bin/mywhois. Use Command+V to paste the above text into pico, and save and exit. Now, just make the script executable, by entering sudo chmod +x /usr/bin/mywhois, and you're done!

Now when you run a mywhois macosxhints.com, for example, the first few lines will show:
 Getting server...
Searching server: whois.godaddy.com
The data contained in Go Daddy Software, Inc.'s WHOIS database,
while believed by the company to be reliable, is provided "as is"...
[Editor's note: I tested this an it ran just fine as is in my relatively standard tcsh shell environment.]
    •    
  • Currently 1.67 / 5
  You rated: 3 / 5 (3 votes cast)
 
[11,926 views]  

Create a one-step whois lookup script | 17 comments | Create New Account
Click here to return to the 'Create a one-step whois lookup script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Works...
Authored by: mholve on Jan 29, '03 10:58:47AM

It works in your tcsh environment because it's a bash script (calling bash on the first line). So regardless of your shell, this script should work.

The script works nicely - thanks! :)



[ Reply to This | # ]
paths problem
Authored by: Anonymous on Jan 29, '03 12:11:54PM

When I type 'mywhois apple.com' I get: "mywhois: Command not found." But if I type the whole path to the script, it runs just fine. I seem to remember there being a shell preference I can set to have it automagically look in /usr/bin/ for commands, but I can't remember how to do it. Can someone refresh my memory?



[ Reply to This | # ]
paths problem
Authored by: Anonymous on Jan 29, '03 12:15:48PM

whoops. Nevermind I remembered it. For those who don't know, you can create a .tshrc file in your home directory with this line:

setenv PATH /usr/bin:$PATH

or

setenv PATH /usr/local/bin:$PATH

for stuff there. Works great, though I don't know if this is the way you're "supposed" to do it.



[ Reply to This | # ]
Use 'rehash'
Authored by: owain_vaughan on Jan 30, '03 06:41:50AM

/usr/bin should already be in your PATH, but in C-type shells you need to do a 'rehash' after putting new executables in a PATH directory. As a matter of neatness, non-system executables should go in /usr/local/bin anyway :)



[ Reply to This | # ]
Don't change your path!
Authored by: mholve on Jan 30, '03 09:38:34AM

You don't want to muck with your path, and don't have to.

To execute this script, wherever it is, use "./mywhois foo.com" or put the script somewhere that IS in your path, preferably something like "/usr/local/bin."

Make sure that once the script is entered, to do a "chmod 755 mywhois" to make it executable.



[ Reply to This | # ]
equivalent 1-liners
Authored by: mkhaw on Jan 29, '03 12:52:21PM

Some more directly 1-step alternatives:

1) (t)csh alias (typed all on one line):
alias whois "\whois -h "'`\whois \!:1 | sed -n '"s'/.*Whois Server: //p'"'` \!:1'

The funky mix of double- and single-quotes is to get around shell expansion and interpretation of special characters during the definition of the alias itself.

2) Equivalent /bin/sh 'mywhois' script:
#!/bin/sh
whois -h `whois $1 | sed -n 's/.*Whois Server: //p'` $1

3) Equivalent bash 'mywhois' shell function:
mywhois () {
whois -h `whois $1 | sed -n 's/.*Whois Server: //p'` $1
}

In (2) and (3) if you replace the 'whois' within the pipe with the full pathname of the real whois program (e.g., /usr/bin/whois) you can name the script/shell function itself 'whois'.



[ Reply to This | # ]
Better script
Authored by: THEM on Jan 29, '03 12:38:23PM

Here's my quick hack that makes the posted script that much better

#!/bin/sh
echo "Fetching domain's whois server..."
SERVER=`whois $1 | grep -i whois server: | awk '{print $3'}`
if [ $SERVER ]; then
	echo "Querying whois server "$SERVER"..."
	whois -h $SERVER $1
else
	echo "Querying the default whois server..."
	whois $1
fi

If the server actually uses the default whois server the posted script would fail, mine works around that issue.



[ Reply to This | # ]
Better script Bug
Authored by: leen on Jan 29, '03 02:56:10PM

The better script contains a bug, no quotes around the grep arguments

grep -i whois\ server:



[ Reply to This | # ]
Wow
Authored by: Jay on Jan 29, '03 02:46:30PM

Now that is very handy! I always wondered why whois didn't give me the result I wanted. Thanks!



[ Reply to This | # ]
better way
Authored by: see on Jan 29, '03 04:25:52PM

this works for any tld
alias mywhois='/usr/bin/whois -h whois.geektools.com'

$ mywhois domain.foo



[ Reply to This | # ]
better way
Authored by: yosithezet on Jan 30, '03 01:58:58AM

Dind't work for any of the three domains I put in.



[ Reply to This | # ]
better way
Authored by: see on Jan 30, '03 11:59:25AM

then you do something wrong. its a whoisgateway for any tld
(also has a webinterface: http://whois.geektools.com)



[ Reply to This | # ]
Keep in mind
Authored by: Wevah on Jan 30, '03 10:48:22PM

that the alias command is slightly different under tcsh. tcsh doesn't use an = between the alias name and the command:

alias mywhois 'whois -h whois.geektools.com'

then

% mywhois domain.tld



[ Reply to This | # ]
Yet another whois
Authored by: pbx on Jan 29, '03 07:39:40PM

Just to prove how many ways there are to do this, here's the tcsh alias I've been using. It may suffer from the "default server" bug noted above; I haven't encountered it though. It's a one-liner:

alias whoz 'whois -h `whois !:1 | grep "Whois Server:" | sed "s/Whois Server: //" ` !:1'



[ Reply to This | # ]
Another variant...
Authored by: sardu_mac on Jan 30, '03 05:32:57AM

fink install jwhois



[ Reply to This | # ]
none of these work...
Authored by: rkleim on Jan 30, '03 03:22:53PM

All of these get the same response:

[imac-OSX:~] rak% mywhois www.tampabay.rr.com
Getting server...
Searching server:
usage: whois [-h hostname] name ...
[imac-OSX:~] rak%

so what's up?



[ Reply to This | # ]
none of these work...
Authored by: foonie on Jan 31, '03 02:32:14AM

the domain is simply rr.com.
all of the above should return correct data.



[ Reply to This | # ]