What's the IP For?

June 7th, 2008

In the spirit of clogging, here is a short diddy for you. I am constantly looking up ip’s for domains. I don’t know why and can’t present you with a use case but I do it a lot. What I typically do is fire up terminal, ping the domain and then copy the ip to my clipboard. Granted this doesn’t take long, but for the guy who aliases script/console to sc, it seems like ages. Also, I never quite select the text properly and I have to hit ctrl-c to kill ping, etc, etc.

Today I whipped together a tiny ruby script to do all the work for me. Now for some code.

#!/usr/bin/env ruby
require 'resolv'

unless ARGV.size == 1
  puts "-> Usage: ipfor example.com" 
  exit
end

# only works on mac
def paste(str)
  %x[echo "#{str}" | pbcopy]
  puts "-> Pasted #{str} to your clipboard" 
end

begin
  paste(Resolv.getaddress(ARGV.first))
rescue Resolv::ResolvError
  puts "-> Could not resolve '#{ARGV.first}'" 
end

I put this in ~/bin, named it ipfor (without.rb) and chmod u+x ipfor to make it executable. Once you’ve done that (and assuming you have ~/bin in your PATH), you can do the following on the command line:

~ $ ipfor
-> Usage: ipfor example.com
~ $ ipfor railstips.org
-> Pasted 208.113.156.68 to your clipboard
~ $ ipfor railstips
-> Could not resolve 'railstips'

Yep, it automatically resolves the ip and pipes it into the mac command that pastes text to the clipboard. So like I said, nothing special but it saves me a few seconds here and there. Also, it was fun to see what the different ways to get an IP using ruby were. At first I was just using %x with ping and parsing the output. Then it hit me that I probably wasn’t the first person needing to resolve domains, so I looked through ruby core and found the resolv library.

8 Responses to “What's the IP For?”

  1. Don Says:

    Only slightly related, but if you were using ping to do name resolution, check out “dig”. On OSX it’s installable through ports.

    It’s a nice tool to know when you are faced with DNS problems, as it’ll tell you TTL and what nameserver it’s getting answers from, and can be used to query about other records like MX and NS.

  2. Peter Cooper Says:

    nslookup rubyonrails.org

    :)

    It doesn’t do the copying and isn’t clean enough to pipe on a pbcopy, but is easier than both dig and ping.

  3. Adam Wiggins Says:

    I’m fond of:

    host example.com

    Particularly since it shows the CNAME relationships (often interesting) and round-robin IPs.

    You could get round-robin IPs out of your script code like this:

    def ips_for(host)
       Resolv::DNS.open do |dns|
          ress = dns.getresources(host, Resolv::DNS::Resource::IN::A)
          ress.map { |r| r.address.to_s }
       end
    end
    
    puts ips_for('example.com').join(' ')
    
  4. Jesse Newland Says:

    Seconding Adam, host is awesome. Check out the man page for more goodies like host -t mx foo.com

  5. Mathias Stjernström Says:

    I know this is ruby/rails stuff but one can easily pull this off by a simple bash function ->

    function ipfor(){ IP=`host -ta $1 | head -n1 | awk ‘{print $4}’` }

    if [ `echo "${IP}." | grep -E "([0-9]{1,3}\.){4}"` ]
    then
      echo $IP | pbcopy
      echo "-> Pasted $IP to your clipboard" 
    else
     echo "-> Could not resolve $1" 
    fi

    Just throw it in .profile and you have a new shell function ;)

    Cheers!

  6. Joe Van Dyk Says:

    That’s a lot of work for:

    dig tanga.com +short | pbcopy

  7. MilesZS Says:
    If there is anyone who would like this to work in Linux (particularly, Debian-based systems), do this:
    
    sudo apt-get install xsel
    
    Then change the line with ‘pbcopy’ to:
    
     %x[echo "#{str}" | xsel --clipboard]
    

    (Added note: I tend to just alias ‘xsel—clipboard’ to ‘xsel’, as I don’t typically use it for anything else. This will make using it in every day CLI situations simpler.)

  8. John Nunemaker Says:

    Sweet. Awesome tips everyone. Most of the things mentioned I’m not that familiar with so I appreciate the comments.

Sorry, comments are closed for this article.