在 Ruby 中获取 DNS TXT 记录
我需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试安装 dnsruby gem。
该代码得到积极维护,并在一些重要的生产系统中使用。
(在 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.
(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
.使用 Ruby stdlib
Resolv::DNS
库而不安装 gem: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: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, calldata
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
Ruby 提供了“Resolv”一个线程感知的 DNS 解析器库。 Resolv 可以同时处理多个 DNS 请求,而不会阻塞整个 Ruby 解释器。
用于获取 DNS MX 记录
用于获取 DNS A 记录
用于获取 DNS 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
For getting DNS A records
For getting DNS TXT records
尝试 Net::DNS gem。
下面是一个示例:
有关安装和使用的更多说明可以在上面链接的 Github 页面上找到。
Try the Net::DNS gem.
Here is an example:
Further instructions for installation and usage can be found on the Github page linked above.
或者使用 system("nslookup -q=txt xxxx.com")
Or use system("nslookup -q=txt xxxx.com")