Skip to content

Instantly share code, notes, and snippets.

@dragolabs
Last active August 29, 2015 14:11
Show Gist options
  • Save dragolabs/faa2d02bdb4309879ba3 to your computer and use it in GitHub Desktop.
Save dragolabs/faa2d02bdb4309879ba3 to your computer and use it in GitHub Desktop.
Find location of server by local ip address
require 'socket'
def have_ip?(ip)
ip_arr = Socket.ip_address_list.select{|intf| intf.ipv4?}
ip_arr.to_s.scan(/(?<=: )[\d.]+(?=>)/).find{ |e| /#{ip}/ =~ e }
end
if File.exist?('/var/local/location.txt')
dc_location = File.read('/var/local/location.txt')
elsif have_ip?("172.16")
dc_location = 'dc2'
elsif have_ip?("192.168.10")
dc_location = 'office'
elsif have_ip?("192.168.255")
dc_location = 'home'
else
dc_location = 'unknown'
end
puts dc_location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment