Last active
August 29, 2015 14:11
-
-
Save dragolabs/faa2d02bdb4309879ba3 to your computer and use it in GitHub Desktop.
Find location of server by local ip address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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