Net::DNS - Perl interface to the DNS resolver
Net::DNS is a collection of Perl modules to interface with the Domain Name System (DNS) resolver. It allows the programmer to perform queries that are beyond the capabilities of gethostbyname and gethostbyaddr.
gethostbyname
gethostbyaddr
A resolver object is an instance of the Net::DNS::Resolver class. A program can have multiple resolver objects, each maintaining its own state information such as the nameservers to be queried, whether recursion is desired, etc.
Net::DNS::Resolver
Net::DNS::Resolver queries return Net::DNS::Packet objects. Packet objects have five sections:
Net::DNS::Packet
The header section, a Net::DNS::Header object.
Net::DNS::Header
The question section, a list of Net::DNS::Question objects.
Net::DNS::Question
The answer section, a list of Net::DNS::RR objects.
Net::DNS::RR
The authority section, a list of Net::DNS::RR objects.
The additional section, a list of Net::DNS::RR objects.
Net::DNS::Header objects represent the header section of a DNS packet.
Net::DNS::Question objects represent the query section of a DNS packet.
Net::DNS::RR is the base class for DNS resource record (RR) objects in the answer, authority, and additional sections of a DNS packet.
print Net::DNS->version;
Returns the version of Net::DNS.
See the manual pages listed above for class-specific methods.
The following examples show how to use the DNS modules. Please note that most of the examples are simple and expect a successful query and certain record types. See the demo scripts included with the source code for examples of more robust code.
# # Look up a host's addresses. # use Net::DNS; $res = new Net::DNS::Resolver; $query = $res->search("foo.bar.com"); foreach $record ($query->answer) { print $record->address, "\n"; } # # Find the nameservers for a domain. # use Net::DNS; $res = new Net::DNS::Resolver; $query = $res->query("foo.com", "NS"); foreach $nameserver ($query->answer) { print $nameserver->nsdname, "\n"; } # # Find the MX records for a domain. # use Net::DNS; $res = new Net::DNS::Resolver; $query = $res->query("foo.com", "MX"); foreach $mxhost ($query->answer) { print $mxhost->preference, " ", $mxhost->exchange, "\n"; } # # Print a domain's SOA record in zone file format. # use Net::DNS; $res = new Net::DNS::Resolver; $query = $res->query("foo.com", "SOA"); ($query->answer)[0]->print; # # Perform a zone transfer and print all the records. # use Net::DNS; $res = new Net::DNS::Resolver; $res->nameservers("ns.foo.com"); @zone = $res->axfr("foo.com"); foreach $rr (@zone) { $rr->print; } # # Send a background query and do some other processing while # waiting for the answer. # use Net::DNS; $res = new Net::DNS::Resolver; $socket = $res->bgsend("foo.bar.com"); until ($res->bgisready($socket)) { # do some work here # ...and some more here } $packet = $res->bgread($socket); $packet->print; # # Send a background query and use select() to determine when the answer # has arrived. # use Net::DNS; $res = new Net::DNS::Resolver; $socket = $res->bgsend("foo.bar.com"); $rin = ""; vec($rin, $socket->fileno, 1) = 1; # Add more descriptors to $rin if desired. $timeout = 5; $nfound = select($rout=$rin, undef, undef, $timeout); if ($nfound < 1) { print "timed out after $timeout seconds\n"; } elsif (vec($rout, $socket->fileno, 1) == 1) { $packet = $res->bgread($socket); $packet->print; } else { # Check for the other descriptors. }
Copyright (c) 1997 Michael Fuhr. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
perl(1), Net::DNS::Resolver, Net::DNS::Packet, Net::DNS::Header, Net::DNS::Question, Net::DNS::RR, RFC 1035
To install Net::DNS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::DNS
CPAN shell
perl -MCPAN -e shell install Net::DNS
For more information on module installation, please visit the detailed CPAN module installation guide.