百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

net-dns

> 编程语言
开源

Net::DNS 是一款用 Ruby 编写的 DNS 库。

167 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Net::DNS 是一款用 Ruby 编写的 DNS 库。

Net::DNS

Net::DNS is a DNS library written in pure Ruby. It started as a port of Perl Net::DNS module, but it evolved in time into a full Ruby library.

Features

  • Complete OO interface
  • Clean and intuitive API
  • Modular and flexible

Requirements

Ruby >= 2.6

Installation

The best way to install this library is via RubyGems.

gem install net-dns

API Documentation

Visit the page http://rdoc.info/gems/net-dns

Trivial resolver

The simplest way to use the library is to invoke the Resolver() method:

ruby
require 'rubygems' 
require 'net/dns'
p Resolver("www.google.com")

The output is compatible with BIND zone files and it's the same you would get with the dig utility.

…

An optional block can be passed yielding the Net::DNS::Packet object

ruby
Resolver("www.google.com") { |packet| puts packet.size + " bytes" }
# => 484 bytes

Same for Net::DNS::Resolver.start():

ruby
Net::DNS::Resolver.start("google.com").answer.size
# => 5

As optional parameters, +TYPE+ and +CLASS+ can be specified.

…

Handling the response packet

The method Net::DNS::Resolver.start is a wrapper around Resolver.new. It returns a new Net::DNS::Packet object.

A DNS packet is divided into 5 sections:

  • header section # => a Net::DNS::Header object
  • question section # => a Net::DNS::Question object
  • answer section # => an Array of Net::DNS::RR objects
  • authority section # => an Array of Net::DNS::RR objects
  • additional section # => an Array of Net::DNS::RR objects

You can access each section by calling the attribute with the same name on a Packet object:

ruby
packet = Net::DNS::Resolver.start("google.com")

header = packet.header
answer = packet.answer

puts "The packet is #{packet.data.size} bytes"
puts "It contains #{header.anCount} answer entries"

answer.any? {|ans| p ans}

The output is

The packet is 378 bytes
It contains 3 answer entries
google.com.             244     IN      A       74.125.45.100
google.com.             244     IN      A       74.125.67.100
google.com.             244     IN      A       209.85.171.100

A better way to handle the answer section is to use the iterators directly on a Packet object:

ruby
packet.each_address do |ip|
  puts "#{ip} is alive" if Ping.pingecho(ip.to_s, 10, 80)
end

Gives:

74.125.45.100 is alive
74.125.67.100 is alive
209.85.171.100 is alive

License

Net::DNS is distributed under the same license Ruby is.

Authors

  • Marco Ceresa (@bluemonk)
  • Simone Carletti (@weppos)

Issues· 16 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言