如何得到Rails的本地ip地址

阅读更多
都是基于linux平台的

典型的不好的办法是
`ifconfig`.slice(/[\d|.]+  Bcast/).sub(/  Bcast/,'')



人家比较好的办法是
  ###############
  # local_ip
  # This is to get around using ifconfig shell calls to get an ip address
  # Described here
  #http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
  ###############
  require 'socket'
  def get_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  #google的ip
      s.addr.last  
    end  
  ensure  
    Socket.do_not_reverse_lookup = orig  
  end  

你可能感兴趣的:(Rails,Socket,Ruby,WordPress,Linux)