Ruby 2.4

Resolv

Resolv类

Parent:Object

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不受支持。

常量

AddressRegex

请使用Regexp来匹配IP地址。

DefaultResolver

用于Resolv类方法的默认解析器。

公共类方法

each_address(name, &block) Show source

遍历所有IP地址的名称。

# File lib/resolv.rb, line 57 def self.each_address(name, &block) DefaultResolver.each_address(name, &block) end

each_name(address, &proc) Show source

遍历所有主机名的地址。

# File lib/resolv.rb, line 78 def self.each_name(address, &proc) DefaultResolver.each_name(address, &proc) end

getaddress(name) Show source

查找名称的第一个IP地址。

# File lib/resolv.rb, line 43 def self.getaddress(name) DefaultResolver.getaddress(name) end

getaddresses(name) Show source

查找所有IP地址的名称。

# File lib/resolv.rb, line 50 def self.getaddresses(name) DefaultResolver.getaddresses(name) end

getname(address) Show source

查找地址的主机名。

# File lib/resolv.rb, line 64 def self.getname(address) DefaultResolver.getname(address) end

getnames(address) Show source

查找所有地址的主机名。

# File lib/resolv.rb, line 71 def self.getnames(address) DefaultResolver.getnames(address) end

new(resolvers=Hosts.new, DNS.new) Show source

使用解析器创建新的Resolv。

# File lib/resolv.rb, line 85 def initialize(resolvers=[Hosts.new, DNS.new]) @resolvers = resolvers end

公共实例方法

each_address(name) { |name| ... } Show source

遍历所有IP地址的名称。

# File lib/resolv.rb, line 109 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| ... } Show source

遍历所有主机名的地址。

# File lib/resolv.rb, line 144 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) Show source

查找名称的第一个IP地址。

# File lib/resolv.rb, line 92 def getaddress(name) each_address(name) {|address| return address} raise ResolvError.new("no address for #{name}") end

getaddresses(name) Show source

查找所有IP地址的名称。

# File lib/resolv.rb, line 100 def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end

getname(address) Show source

查找地址的主机名。

# File lib/resolv.rb, line 127 def getname(address) each_name(address) {|name| return name} raise ResolvError.new("no name for #{address}") end

getnames(address) Show source

查找所有地址的主机名。

# File lib/resolv.rb, line 135 def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end