如何在Ruby on Rails中获取远程主机名?

时间:2013-07-12 20:12:07

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

我希望在Ruby on Rails应用程序中解析远程客户端的主机名。

我知道Request.remote_ip会给我客户端的IP地址,但是如何将其转换为主机名呢?

2 个答案:

答案 0 :(得分:6)

我明白了!

这是我在ApplicationHelper中添加的方法:

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

这很简单!

答案 1 :(得分:-1)

尝试如下;

require 'socket'
host = Socket.gethostname

或者你可以简单地做;

`hostname`.strip # Get the hostname from the shell and removing trailing \n