3
我正在尋找解決Ruby on Rails應用程序中的遠程客戶端的主機名。如何在Ruby on Rails中獲取遠程主機名?
我知道Request.remote_ip
會給我客戶端的IP地址,但是如何將它轉換爲他們的主機名呢?
我正在尋找解決Ruby on Rails應用程序中的遠程客戶端的主機名。如何在Ruby on Rails中獲取遠程主機名?
我知道Request.remote_ip
會給我客戶端的IP地址,但是如何將它轉換爲他們的主機名呢?
我想通了!
這裏我把我的ApplicationHelper的方法:
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
我認爲他是*遠程*主機名後,而不是本地的。 –