She sells zsh’s by the C-shore

21 Jun
2013-06-21

Okay, bad pun asides, I’m going to show you a neat little zsh trick that significantly improves the whois output on Danish .dk domains.
I assume you already know zsh, which is objectively the most awesome unix shell in all of history. If you don’t, go get it now! Then come back here.

Okay, the problem is the following. If you do a whois query for a danish domain name, you’ll get some output like the following:

$ whois eksempel.dk
# Copyright (c) 2002 - 2013 by DK Hostmaster A/S
# The data in the DK Whois ... (cut for brevity)
Domain:               eksempel.dk
DNS:                  eksempel.dk
Registered:           1999-05-17
Expires:              2017-06-30
Registration period:  5 years
VID:                  yes
Status:               Active

Nameservers
Hostname:             auth01.ns.dk-hostmaster.dk
Hostname:             auth02.ns.dk-hostmaster.dk

# Use option --show-handles to get handle information.
# Whois HELP for more help.

It’s a perfectly fine whois response, except that the handles are hidden by default. They even say as much in a comment.

So how do we actually provide “–show-handles” to whois? Well, the obvious way doesn’t work:

$ whois --show-handles eksempel.dk
/usr/bin/whois: unrecognized option '--show-handles'
Usage: whois [OPTION]... OBJECT...
...

Well, fudge. Digging deeper, it turns out that “–show-handles” is actually an argument for the query, not a standard argument for the whois program itself.

So how do we control the query parameters? Well, the reason that the previous attempt failed, is that whois cannot discern the query parameters (–show-handles) the program parameters (none, in this case).

The man page says:

When querying a non-standard server, command line options which are not to be interpreted by the client should always follow the — separator (which marks the beginning of the query string).

Okay, so we should try “–” to make it understand that we want –show-handles:

$ whois eksempel.dk -- --show-handles
No whois server is known for this kind of object.

Wow, this is just.. Well, it turns out that it now thinks the whole query is for “–show-handles”, which of course is wrong. With a bit of quick thinking, we arrive at the final, working, solution:

$ whois -- --show-handles eksempel.dk
# Copyright (c) 2002 - 2013 by DK Hostmaster A/S
# The data in the DK Whois ... (cut for brevity)
Domain: eksempel.dk
DNS: eksempel.dk
Registered: 1999-05-17
Expires: 2017-06-30
Registration period: 5 years
VID: yes
Status: Active

Registrant
Handle: DKHM1-DK
Name: DK Hostmaster A/S
Address: Kalvebod Brygge 45, 3
Postalcode: 1560
City: København V
Country: DK
Phone: +45 33 64 60 60

Administrator
Handle: DKHM1-DK
Name: DK Hostmaster A/S
Address: Kalvebod Brygge 45, 3
Postalcode: 1560
City: København V
Country: DK
Phone: +45 33 64 60 60

Nameservers
Hostname: auth01.ns.dk-hostmaster.dk
Handle: DKHM1-DK
Hostname: auth02.ns.dk-hostmaster.dk
Handle: DKHM1-DK

Neat! Now it works. Only problem is, it’s a bit cumbersome to type, and you can’t put these options in unconditionally with an alias, since this only works for .dk domains.

So instead, we perform this little trick:

if [[ -x /usr/bin/whois ]]; then
  function whois() {
    local WHOIS=/usr/bin/whois
    if [[ ${1##*.} == 'dk' ]]; then
      $WHOIS -- --show-handles $1
    else
      $WHOIS $1
    fi
  }
fi

This code installs a function called “whois” if you have the whois-program installed. This function calls /usr/bin/whois with the –show-handles option for Danish domains only. Other domains get treated normally. It’s ready to copy-paste into your .zshrc file. Enjoy!

VN:F [1.9.22_1171]
Rating: 10.0/10 (2 votes cast)
She sells zsh's by the C-shore, 10.0 out of 10 based on 2 ratings
6 replies
  1. Anders Eiler says:

    And of course… Remember to check if whois is actually installed on your system before you try out this neat trick… 😉

    VN:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
    Reply
    • Christian says:

      Actually, I adressed that problem before writing this post. Check again 😉

      If you’re wondering, what Anders is referring to here, it’s the fact that if you define the whois function without having the whois binary installed, you get a somewhat confusing error message:

      $ whois example.org
      whois:5: no such file or directory: /usr/bin/whois
      
      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      Reply
  2. David says:

    actually this works just as fine in bash. Not to say that you should use bash (i use zsh myself and have so for year) however it’s not a zsh feature as such.
    btw in case you need to do a reverse lookup on the ip a domain is pointed at. (or a list)

    atserver(){
      for i in $* ; do
        IP=$(dig +short $i @ns01.one.com| grep -v ";")
        echo $i @ $(dig +short -x $IP | grep -v ";")
      done
    }
    
    VA:F [1.9.22_1171]
    Rating: 4.0/5 (1 vote cast)
    Reply
    • Christian says:

      Very true. It’s just that I exclusively use zsh, and I didn’t want to call it “tips that work perfectly in zsh, and pretty much ok for the most part in other shells”, it’s just not as convinient or catchy 😉

      That being said, you are right of course. With all the shared heritage and syntax of the sh-style shells, a lot of this will work in bash or ksh too.

      VN:F [1.9.22_1171]
      Rating: 4.5/5 (2 votes cast)
      Reply
  3. Raghavendra says:

    Which whois client is this.

    For:
    Name : whois
    Version : 5.0.25-2
    Description : The whois client by Marco d’Itri
    Architecture : x86_64
    URL : http://www.linux.it/~md/software/

    I get registrants without –show-handles and it says it is
    logging my ip 🙂 – http://paste.wnohang.net/8d40ca

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    Reply
    • Christian says:

      Wow, this is a surprive for me.

      It turns out recent-ish versions in Ubuntu and Debian have fixed this “feature” inside the whois binary itself. So now it’s intelligent enough to know how to query .dk names.

      So yeah, just ignore my whole post if you run a recent version of whois. Dang 😉

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright - Christian Iversen's blog
UA-41598088-1