Resolv 類別
Resolv
是使用 Ruby 編寫的多執行緒 DNS
解析器程式庫。 Resolv
可以同時處理多個 DNS
要求,而不會阻擋整個 Ruby 詮譯器。
另請參閱 resolv-replace.rb,以使用 Resolv
取代 libc 解析器。
Resolv
可以直接使用 DNS
模組查詢各種 DNS
資源。
範例
p Resolv.getaddress "www.ruby-lang.org" p Resolv.getname "210.251.121.214" Resolv::DNS.open do |dns| ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A p ress.map(&:address) ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX p ress.map { |r| [r.exchange.to_s, r.preference] } end
錯誤¶ ↑
-
不支援 NIS。
-
不支援 /etc/nsswitch.conf。
常數
公開類別方法
each_address(name, &block) 按一下以切換來源
反覆處理 name
的所有 IP 位址。
# File lib/resolv.rb, line 59 def self.each_address(name, &block) DefaultResolver.each_address(name, &block) end
each_name(address, &proc) 按一下以切換來源
反覆處理 address
的所有主機名稱。
# File lib/resolv.rb, line 80 def self.each_name(address, &proc) DefaultResolver.each_name(address, &proc) end
getaddress(name) 按一下以切換來源
查詢 name
的第一個 IP 位址。
# File lib/resolv.rb, line 45 def self.getaddress(name) DefaultResolver.getaddress(name) end
getaddresses(name) 按一下以切換來源
查詢 name
的所有 IP 位址。
# File lib/resolv.rb, line 52 def self.getaddresses(name) DefaultResolver.getaddresses(name) end
getname(address) 按一下以切換來源
查詢 address
的主機名稱。
# File lib/resolv.rb, line 66 def self.getname(address) DefaultResolver.getname(address) end
getnames(address) 按一下以切換來源
查詢所有 address
的主機名稱。
# File lib/resolv.rb, line 73 def self.getnames(address) DefaultResolver.getnames(address) end
new(resolvers=nil, use_ipv6: nil) 按一下以切換來源
使用 resolvers
建立新的 Resolv
。
# File lib/resolv.rb, line 87 def initialize(resolvers=nil, use_ipv6: nil) @resolvers = resolvers || [Hosts.new, DNS.new(DNS::Config.default_config_hash.merge(use_ipv6: use_ipv6))] end
公開實例方法
each_address(name) { |name| ... } 按一下以切換來源
反覆處理 name
的所有 IP 位址。
# File lib/resolv.rb, line 111 def each_address(name) if AddressRegex =~ name yield name return end yielded = false @resolvers.each {|r| r.each_address(name) {|address| yield address.to_s yielded = true } return if yielded } end
each_name(address) { |name| ... } 按一下以切換來源
反覆處理 address
的所有主機名稱。
# File lib/resolv.rb, line 146 def each_name(address) yielded = false @resolvers.each {|r| r.each_name(address) {|name| yield name.to_s yielded = true } return if yielded } end
getaddress(name) 按一下以切換來源
查詢 name
的第一個 IP 位址。
# File lib/resolv.rb, line 94 def getaddress(name) each_address(name) {|address| return address} raise ResolvError.new("no address for #{name}") end
getaddresses(name) 按一下以切換來源
查詢 name
的所有 IP 位址。
# File lib/resolv.rb, line 102 def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end
getname(address) 按一下以切換來源
查詢 address
的主機名稱。
# File lib/resolv.rb, line 129 def getname(address) each_name(address) {|name| return name} raise ResolvError.new("no name for #{address}") end
getnames(address) 按一下以切換來源
查詢所有 address
的主機名稱。
# File lib/resolv.rb, line 137 def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end