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

3

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

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

2个回答

6
我已经理解了!
这是我在ApplicationHelper中添加的一个方法:
def remote_hostname
  require 'resolv'
  Resolv.getname(request.remote_ip)
end

就是这么简单!

-1

请尝试以下方法:

require 'socket'
host = Socket.gethostname

或者你可以简单地这样做;

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

1
我认为他想要远程主机名,而不是本地的。 - mu is too short

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接