我希望在 Ruby on Rails 应用程序中解析远程客户端的主机名。
我知道 Request.remote_ip
可以给出客户端的 IP 地址,但是如何将其转换为他们的主机名呢?
我希望在 Ruby on Rails 应用程序中解析远程客户端的主机名。
我知道 Request.remote_ip
可以给出客户端的 IP 地址,但是如何将其转换为他们的主机名呢?
def remote_hostname
require 'resolv'
Resolv.getname(request.remote_ip)
end
请尝试以下方法:
require 'socket'
host = Socket.gethostname
或者你可以简单地这样做;
`hostname`.strip # Get the hostname from the shell and removing trailing \n