Getting the Hostname or Ip in Ruby on Rails

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"

Getting IP address from hostnames in Ruby

That's just how the getaddrinfo method works. If the name can be resolved you get an IP address expressed as a string. If not you get a SocketError exception. That means in order to handle these you need to anticipate that:

server_names.map do |name|
begin
IPSocket.getaddress(name)
rescue SocketError
false # Can return anything you want here
end
end

Note that when calling methods on things it's convention to use . and not the namespace separator ::. The separator does work but it's messy as that's usually reserved for referencing constants like MyClass::MY_CONST.

How to get client IP and Server IP using Rails

Thanks: karim79 and Titanous.

Write the code in Controller

For Client IP:

request.remote_ip

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

For Server IP:

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

How to get the remote hostname in Ruby on Rails?

I figured it out!

Here's a method I put in my ApplicationHelper:

def remote_hostname
require 'resolv'
Resolv.getname(request.remote_ip)
end

Simple as that!

How to get Host name in Rails 3?

You have several options:

  • call system's hostname command (assuming it's *nix): `hostname`
  • use the socket gem:

    require 'socket'

    Socket.gethostname

  • in case you just want to know the current domain the request visits: request.domain

Update

From the request object, we can fetch both the host and the port, please check out these methods:

  • request.protocol
  • request.host
  • request.port

You could build the full url from these methods.

From my personal experience, for most projects you might have a fixed domain for each environment, and usually configure that in a yaml file or so (for example, to send email and use urls with domain name in the email body, we usually read the config and set options for url helper). So I usually just read the current environment's urls configuration and use them from the code.

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

Rails get client's name of pc

es There is many solution

1) require 'socket'

Socket.gethostname

2) system("hostname")

3) If you want to get request host then request.host

If you want remote hostname

4) require 'resolv'

Resolv.getname(request.remote_ip)

Thanks



Related Topics



Leave a reply



Submit