A Hexdump extension to the String class in Ruby Extending Watir for Custom Error Pages
Dec 26

Sometimes its nice to be able to  find out what IP address you are using to get to the Internet for things like setting up servers on the correct interface.  I use the following code to find this out:


require 'open-uri'

def myINETaddr
  ip1 = open("").read[1..20]
  ip2 = open("").read[/IP Address\: (.*)\<\/body/, 1]
  if ip1 != ip2
    throw "myINETaddr::UnableToRetrieve"
  return ip1

if __FILE__ == $0
  puts myINETaddr

I have it check two outside services just to make sure they both agree…otherwise I have it throw an error. Of course, if you are behind a proxy server or NAT device, this method is just going to return the IP address of that device. If you just want the IP address of your default network interface, this code does the job just fine:

require 'socket'

def myIPaddr

if __FILE__ == $0
   puts myIPaddr
SociBook Digg Facebook Google Yahoo Buzz StumbleUpon

Leave a Reply