Reputation: 48443
I am trying to get IP address of server, where is running my app. In PHP exists the function called GetHostByName()
(or something like that), what is the alternative for Ruby?
Upvotes: 1
Views: 2324
Reputation: 51421
You can use this (no shell required):
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
Output:
# irb:0> local_ip
# => "192.168.0.1"
I use this function, but credit goes to: http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
Upvotes: 3