Skip to content

Instantly share code, notes, and snippets.

@jasonmadigan
Created September 7, 2009 16:08
Show Gist options
  • Save jasonmadigan/182423 to your computer and use it in GitHub Desktop.
Save jasonmadigan/182423 to your computer and use it in GitHub Desktop.
Local IP lookup
#!/usr/bin/env ruby
require 'socket'
class GetIP
def self.local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
UDPSocket.open do |s|
s.connect '64.233.187.99', 1 # Relax, nothing is hitting the network
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
end
if $0 == __FILE__
puts GetIP.local_ip
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment