开发者

Getting a DNS TXT record in Ruby

开发者 https://www.devze.com 2023-01-01 16:11 出处:网络
开发者_Go百科I need to get the txt field from the DNS record. Is there any ruby api to do something like this?

开发者_Go百科I need to get the txt field from the DNS record.

Is there any ruby api to do something like this?

nslookup -q=txt xxxx.com


Try installing the dnsruby gem.

The code is actively maintained, and used in some significant production systems.

require 'rubygems'
require 'dnsruby'
include Dnsruby

# Use the system configured nameservers to run a query
res = Dnsruby::Resolver.new
ret = res.query("google.com", Types.TXT)
print ret.answer

(Code tested on MacOS X - prints the Google SPF record)

See also @Alex's answer which is more idiomatic Ruby - Alex is the author of dnsruby.


Use the Ruby stdlib Resolv::DNS library without installing a gem:

require 'resolv'
txt = Resolv::DNS.open do |dns|
  records = dns.getresources("_dmarc.yahoo.com", Resolv::DNS::Resource::IN::TXT)
  records.empty? ? nil : records.map(&:data).join(" ")
end
#=> "v=DMARC1; p=reject; sp=none; pct=100; rua=mailto:dmarc-yahoo-rua@yahoo-inc.com, mailto:dmarc_y_rua@yahoo.com;"

getresources returns an array of instances of the requested record class name (Resolv::DNS::Resource::IN::TXT). Here, I return nil if the TXT record(s) or the host name were not found, otherwise I map over the records, call data to get the values, then join them together.

Any DNS record type [TXT, NS, CNAME, MX, ...] can be queried as well by replacing TXT in the above example.

TXT records are "unstructured" and used for enhanced data for the hostname such as SPF, DKIM, DMARC configurations. In practice, there may be only one TXT record, but the RFC doesn't say how many there can be.

Read the docs at: http://www.ruby-doc.org/stdlib-2.1.1/libdoc/resolv/rdoc/index.html


require 'dnsruby'
Dnsruby::DNS.open {|dns|
  dns.each_resource("google.com", "TXT") {|rr| print rr}
    # or
  print dns.getresource("google.com", "TXT")}
}


Ruby provides "Resolv" a thread-aware DNS resolver library. Resolv can handle multiple DNS requests concurrently without blocking the entire Ruby interpreter.

For getting DNS MX records

require "resolv"
    Resolv::DNS.open do |dns|
      ress = dns.getresources "infoir.com", Resolv::DNS::Resource::IN::MX
      p ress.map { |r| [r.exchange.to_s, r.preference] }
    end

For getting DNS A records

require "resolv"
    Resolv::DNS.open do |dns|
      ress = dns.getresources "infoir.com", Resolv::DNS::Resource::IN::A
      p ress.map { |r| [r.exchange.to_s, r.preference] }
    end

For getting DNS TXT records

require "resolv"
Resolv::DNS.open do |dns|
  ress = dns.getresources "infoir.com", Resolv::DNS::Resource::IN::TXT
  p ress.map { |r| [r.exchange.to_s, r.preference] }
end


Try the Net::DNS gem.

Here is an example:

result = Net::DNS::Resolver.start("google.com", Net::DNS::TXT)
values = result.each_mx.map { |r| r.txt }
# "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all "

Further instructions for installation and usage can be found on the Github page linked above.


Or use system("nslookup -q=txt xxxx.com")

0

精彩评论

暂无评论...
验证码 换一张
取 消