I need to get my IP (that is DHCP). I use this in my environment.rb
:
LOCAL_IP = `ifconfig wlan0`.match(/inet addr:(\d*\.\d*\.\d*\.\d*)/)[1] || "localhost"
But is there rubyway or more clean solution?
I need to get my IP (that is DHCP). I use this in my environment.rb
:
LOCAL_IP = `ifconfig wlan0`.match(/inet addr:(\d*\.\d*\.\d*\.\d*)/)[1] || "localhost"
But is there rubyway or more clean solution?
Found here.
A server typically has more than one interface, at least one private and one public.
Since all the answers here deal with this simple scenario, a cleaner way is to ask Socket for the current
ip_address_list()
as in:Both returns an
Addrinfo
object, so if you need a string you can use theip_address()
method, as in:You can easily work out the more suitable solution to your case changing Addrinfo methods used to filter the required interface address.
Here is a small modification of steenslag's solution