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?”
Sorry, comments are closed for this article.

June 7th, 2008 at 06:32 PM
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.
June 7th, 2008 at 07:45 PM
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.
June 7th, 2008 at 09:13 PM
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(' ')June 7th, 2008 at 09:56 PM
Seconding Adam,
hostis awesome. Check out the man page for more goodies likehost -t mx foo.comJune 8th, 2008 at 07:43 AM
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}’` }
Just throw it in .profile and you have a new shell function ;)
Cheers!
June 8th, 2008 at 02:05 PM
That’s a lot of work for:
dig tanga.com +short | pbcopy
June 8th, 2008 at 04:55 PM If there is anyone who would like this to work in Linux (particularly, Debian-based systems), do this: Then change the line with ‘pbcopy’ to:
(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.)
June 8th, 2008 at 06:26 PM
Sweet. Awesome tips everyone. Most of the things mentioned I’m not that familiar with so I appreciate the comments.