Local IP

# encoding: utf-8 require 'socket' # local_ip # return real local IP address # @author Sergey Blohin # @email sblohin@yandex.ru # @example puts local_ip # @return [String] def local_ip # save global do_not_reverse_lookup flag original = Socket.do_not_reverse_lookup # set global do_not_reverse_lookup flag to true Socket.do_not_reverse_lookup = true # open UDP socket # return last IP address UDPSocket.open do |s| # try connect to Google DNS, for example s.connect('8.8.8.8', 1) s.addr.last end ensure # recovery global do_not_reverse_lookup flag Socket.do_not_reverse_lookup = original end $stdout.write local_ip if __FILE__ == $PROGRAM_NAME

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.