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.
- Complete OO interface
- Clean and intuitive API
- Modular and flexible
Ruby >= 2.6
The best way to install this library is via RubyGems.
gem install net-dns
Visit the page http://rdoc.info/gems/net-dns
The simplest way to use the library is to invoke the Resolver() method:
require'rubygems'require'net/dns'pResolver("www.google.com")The output is compatible with BIND zone files and it's the same you would get with the dig utility.
;; Answer received from localhost:53 (212 bytes)
;;
;; HEADER SECTION
;; id = 64075
;; qr = 1 opCode: QUERY aa = 0 tc = 0 rd = 1
;; ra = 1 ad = 0 cd = 0 rcode = NoError
;; qdCount = 1 anCount = 3 nsCount = 4 arCount = 4
;; QUESTION SECTION (1 record):
;; google.com. IN A
;; ANSWER SECTION (3 records):
google.com. 212 IN A 74.125.45.100
google.com. 212 IN A 74.125.67.100
google.com. 212 IN A 209.85.171.100
;; AUTHORITY SECTION (4 records):
google.com. 345512 IN NS ns1.google.com.
google.com. 345512 IN NS ns4.google.com.
google.com. 345512 IN NS ns2.google.com.
google.com. 345512 IN NS ns3.google.com.
;; ADDITIONAL SECTION (4 records):
ns1.google.com. 170275 IN A 216.239.32.10
ns2.google.com. 170275 IN A 216.239.34.10
ns3.google.com. 170275 IN A 216.239.36.10
ns4.google.com. 170275 IN A 216.239.38.10
An optional block can be passed yielding the Net::DNS::Packet object
Resolver("www.google.com"){ |packet| putspacket.size + " bytes"}# => 484 bytesSame for Net::DNS::Resolver.start():
Net::DNS::Resolver.start("google.com").answer.size# => 5As optional parameters, +TYPE+ and +CLASS+ can be specified.
pNet::DNS::Resolver.start("google.com",Net::DNS::MX);;Answerreceivedfromlocalhost:53(316bytes);;;;HEADERSECTION;;id=59980;;qr=1opCode: QUERYaa=0tc=0rd=1;;ra=1ad=0cd=0rcode=NoError;;qdCount=1anCount=4nsCount=4arCount=8;;QUESTIONSECTION(1record):
;;google.com.INMX;;ANSWERSECTION(4records):
google.com.10800INMX10smtp2.google.com.google.com.10800INMX10smtp3.google.com.google.com.10800INMX10smtp4.google.com.google.com.10800INMX10smtp1.google.com.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:
packet=Net::DNS::Resolver.start("google.com")header=packet.headeranswer=packet.answerputs"The packet is #{packet.data.size} bytes"puts"It contains #{header.anCount} answer entries"answer.any?{|ans| pans}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:
packet.each_addressdo |ip|
puts"#{ip} is alive"ifPing.pingecho(ip.to_s,10,80)endGives:
74.125.45.100 is alive
74.125.67.100 is alive
209.85.171.100 is alive
Net::DNS is distributed under the same license Ruby is.
- Marco Ceresa (@bluemonk)
- Simone Carletti (@weppos)