Skip to content

Instantly share code, notes, and snippets.

@aerickson
Created September 16, 2013 18:20
Show Gist options
  • Save aerickson/6584446 to your computer and use it in GitHub Desktop.
Save aerickson/6584446 to your computer and use it in GitHub Desktop.
Get your IP address on unix-y system without a fork in Ruby. http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
require 'socket'
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
UDPSocket.open do |s|
s.connect '64.233.187.99', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment