How to get the IP address of my local machine in Ruby?
Use Socket::ip_address_list.
Socket.ip_address_list #=> Array of AddrInfo
Get own IP address
Try:
require 'socket'
ip=Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
ip.ip_address if ip
Ruby: get local IP (nix)
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:
require 'socket'
def my_first_private_ipv4
Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
end
def my_first_public_ipv4
Socket.ip_address_list.detect{|intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private?}
end
Both returns an Addrinfo
object, so if you need a string you can use the ip_address()
method, as in:
ip= my_first_public_ipv4.ip_address unless my_first_public_ipv4.nil?
You can easily work out the more suitable solution to your case changing Addrinfo methods used to filter the required interface address.
Get real IP address in local Rails development environment
As far as I can see there is no standard method for getting the remote address of your local machine. If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations.
As a last resort you could also hard code your remote address. E.g. like this:
class ApplicationController < ActionController::Base
def remote_ip
if request.remote_ip == '127.0.0.1'
# Hard coded remote address
'123.45.67.89'
else
request.remote_ip
end
end
end
class MyController < ApplicationController
def index
@client_ip = remote_ip()
end
end
Getting the Hostname or IP in Ruby on Rails
From coderrr.wordpress.com:
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
# irb:0> local_ip
# => "192.168.0.127"
To get IP addres of the specific host machine in rub
publish_vm = node['aem_dispatcher_cookbook']['publish'].to_s
ruby_block 'get_ip_from_publish' do
block do
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
command1 = "nslookup #{publish_vm} |grep '^Address' | awk '{print $2}'| tail -1"
command_out = shell_out(command1)
node.run_state['master_ip'] = command_out.stdout
end
action :run
end
This piece of code helped me to get ip address of desired host machine
how to get ip address in rails
To use a development host's external IP address rather than the "localhost" address or loopback address of 127.0.0.1, explicitly bind to the host's external IP address. By default servers may listen on 0.0.0.0 (any address) and the localhost address ends up being used when testing on the same host as the development host.
To bind explicitly using web brick for example:
rails server -b my.real.ip.address
To bind explicitly using apache/passenger or similar, in the apache config use:
Listen my.real.ip.address:port
Get public (remote) IP address
Akamai provides a "What is my IP" page that you can fetch:
require 'open-uri'
remote_ip = open('http://whatismyip.akamai.com').read
There are a few alternatives that do the same thing, though:
- http://whatismyip.akamai.com
- http://ipecho.net/plain
- http://icanhazip.com
- http://ident.me
- http://bot.whatismyipaddress.com
You can also use the ipv4
and ipv6
subdomains with icanhazip.com
.
If you don't want to depend on a third party, you can roll your own in a one-line rack app and deploy this for free on Heroku or whatever. It takes into account that X-Forwarded-For
may contain a comma separated list of proxy IP addresses and only returns the client IP.
# config.ru
run lambda { |env|
remote_ip = env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR']
remote_ip = remote_ip.scan(/[\d.]+/).first
[200, {'Content-Type'=>'text/plain'}, [remote_ip]]
}
Related Topics
Setting Up the Logger in Rails 3
Are There Any Ipython-Like Shells for Ruby or Rails
Copying Gems from Previous Version of Ruby in Rbenv
"Uninitialized Constant" Error When Including a Module
Rails: How to Access Restful Helpers
When Do We Use the "||=" Operator in Rails? What Is Its Significance
Check Whether a String Contains One of Multiple Substrings
Imagemagick 7 with Rmagick 2.16 on MACos Sierra Can't Find Magickwand.H
Generate Unique Random String with Letters and Numbers in Lower Case
Accessing Config from Application.Rb in Controller (Rails 3)
What Is the Use of "#!/Usr/Local/Bin/Ruby -W" at the Start of a Ruby Program
How to Change the Position of an Array Element
How to Log Every Method That's Called in a Ruby Program
How to Step Out of a Loop with Ruby Pry
After Gem Update: Test Fail with "Asset Was Not Declared to Be Precompiled in Production"