2015-10-13 123 views
0

有沒有一種可能的方式來獲取域的主機名在ruby中?在ruby中獲取域名主機名

例如:

$ host api.heroku.com 
api.heroku.com is an alias for api-default.herokussl.com. 
api-default.herokussl.com is an alias for elb027033-298234319.us-east-1.elb.amazonaws.com. 
elb027033-298234319.us-east-1.elb.amazonaws.com has address 23.23.76.65 
elb027033-298234319.us-east-1.elb.amazonaws.com has address 23.21.240.208 
elb027033-298234319.us-east-1.elb.amazonaws.com has address 107.22.242.236 

主機將是最好的例子,因爲它也將展示如果域/子域指向或另一臺主機像amazonAWS等

+0

['Resolv'](http://ruby-doc.org/stdlib-1.9.2/libdoc/resolv/rdoc/Resolv.html) - 該文檔甚至有一些很好的例子。 – Amadan

+0

哪些文檔?我使用Resolv嘗試了一些東西,但沒有像從ruby執行'host domain.com'命令那樣有用。 – user3610137

回答

1

不是有用的紅寶石

我不敢苟同執行host domain.com命令(但它確實涉及知道你想要什麼數據,以及如何DNS的作品)。

require 'resolv' 

def host(address) 
    Resolv::DNS.open do |dns| 
    loop do 
     ress = dns.getresources address, Resolv::DNS::Resource::IN::CNAME 
     break if ress.empty? 
     canonical_name = ress.first.name.to_s 
     puts "#{address} is an alias for #{canonical_name}" 
     address = canonical_name 
    end 

    ress = dns.getresources address, Resolv::DNS::Resource::IN::A 
    addresses = ress.each do |r| 
     puts "#{address} has address #{r.address.to_s}" 
    end 
    end 
    return 
end 

host("api.heroku.com") 
+0

哇!這正是我一直在尋找的!謝謝。 – user3610137