诗歌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
莫愁前路无知己,天下无人不识君。