Skip to content

Instantly share code, notes, and snippets.

Created Jan 19, 2011
What would you like to do?
Return array of local IP addresses
require 'socket'
# Base on answer from
# return an array of local IP addresses
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily do |s|
s.connect '', 1 # as this is UDP, no connection will actually be made {|ip| ip =~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/}.uniq
Socket.do_not_reverse_lookup = orig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment