在 Ruby 中获取 DNS TXT 记录

发布于 2024-09-02 21:14:19 字数 109 浏览 2 评论 0原文

我需要从 DNS 记录中获取 txt 字段。
有没有 ruby​​ api 可以做这样的事情?

nslookup -q=txt xxxx.com

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请帮我爱他 2024-09-09 21:14:19

尝试安装 dnsruby gem。

该代码得到积极维护,并在一些重要的生产系统中使用。

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

(在 MacOS X 上测试的代码 - 打印 Google SPF 记录)

另请参阅 @Alex 的答案,这是更惯用的 Ruby - Alex 是 dnsruby 的作者。

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.

青春有你 2024-09-09 21:14:19

使用 Ruby stdlib Resolv::DNS 库而不安装 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:[email protected], mailto:[email protected];"

getresources 返回请求的记录类名称的实例数组 (Resolv::DNS::资源::IN::TXT)。在这里,如果未找到 TXT 记录或主机名,我将返回 nil,否则我会映射记录,调用 data 获取值,然后将它们连接在一起。

也可以通过替换上例中的 TXT 来查询任何 DNS 记录类型 [TXT、NS、CNAME、MX、...]。

TXT 记录是“非结构化”的,用于主机名的增强数据,例如 SPF、DKIM、DMARC 配置。实际上,可能只有一条 TXT 记录,但 RFC 并没有说明可以有多少条。

阅读文档: http://www.ruby-doc.org /stdlib-2.1.1/libdoc/resolv/rdoc/index.html

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:[email protected], mailto:[email protected];"

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

漫漫岁月 2024-09-09 21:14:19
require 'dnsruby'
Dnsruby::DNS.open {|dns|
  dns.each_resource("google.com", "TXT") {|rr| print rr}
    # or
  print dns.getresource("google.com", "TXT")}
}
require 'dnsruby'
Dnsruby::DNS.open {|dns|
  dns.each_resource("google.com", "TXT") {|rr| print rr}
    # or
  print dns.getresource("google.com", "TXT")}
}
栀子花开つ 2024-09-09 21:14:19

Ruby 提供了“Resolv”一个线程感知的 DNS 解析器库。 Resolv 可以同时处理多个 DNS 请求,而不会阻塞整个 Ruby 解释器。

用于获取 DNS MX 记录

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

用于获取 DNS A 记录

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

用于获取 DNS TXT 记录

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

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
挽袖吟 2024-09-09 21:14:19

尝试 Net::DNS gem。

下面是一个示例:

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 "

有关安装和使用的更多说明可以在上面链接的 Github 页面上找到。

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.

心房敞 2024-09-09 21:14:19

或者使用 system("nslookup -q=txt xxxx.com")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文