Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Ruby one-liner to find external IP address across NAT router. (See related posts)

Sometimes you just have to know what your external IP address is when there's a NAT between you and the rest of the world. Say you're in a DHCP environment that's constantly changing your IP, for example. This one-liner gives you your IP address as a string (in this case stored as "my_ip").

my_ip = (require 'open-uri' ; open("http://myip.dk") { |f| /([0-9]{1,3}\.){3}[0-9]{1,3}/.match(f.read)[0].to_a[0] })

Comments on this post

banana989 posts on May 02, 2007 at 12:43
Wow- cool !
martinx posts on May 02, 2007 at 23:29
how about this:
{|f|f.read.scan(/([0-9]{1,3}\.){3}[0-9]{1,3}/);puts $~}

You need to create an account or log in to post comments to this site.


Click here to browse all 4863 code snippets

Related Posts