Net::DNS (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
Net::DNS - Perl Interface to the Domain Name SystemSYNOPSIS
use Net::DNS;
DESCRIPTION
Net::DNS is a collection of Perl modules that act as a Domain Name System (The programmer should be somewhat familiar with the format of a
Resolver Objects
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.Packet Objects
Net::DNS::Resolver queries return Net::DNS::Packet objects. Packet objects have five sections:- *
- The header section, a Net::DNS::Header object.
- *
- The question section, a list of Net::DNS::Question objects.
- *
- The answer section, a list of Net::DNS::RR objects.
- *
- The authority section, a list of Net::DNS::RR objects.
- *
- The additional section, a list of Net::DNS::RR objects.
Update Objects
Net::DNS::Update is a subclass of Net::DNS::Packet used to create dynamic update requests.Header Objects
Net::DNS::Header objects represent the header section of aQuestion Objects
Net::DNS::Question objects represent the content of the question section of aRR Objects
Net::DNS::RR is the base class for Do not assume that
METHODS
See the manual pages listed above for other class-specific methods.version
print Net::DNS->version, "\n";
Returns the version of Net::DNS.
mx
# Use a default resolver -- can not get an error string this way. use Net::DNS; my @mx = mx("example.com"); # Use your own resolver object. use Net::DNS; my $res = Net::DNS::Resolver->new; my @mx = mx($res, "example.com");
Returns a list of Net::DNS::RR::MX objects representing the
This method does not look up A records; it only performs
See ``
Dynamic DNS Update Support
The Net::DNS module provides auxiliary functions which support dynamicyxrrset
Use this method to add an ``RRset exists'' prerequisite to a dynamic update packet. There are two forms, value-independent andvalue-dependent:
# RRset exists (value-independent) $update->push(pre => yxrrset("host.example.com A"));
Meaning: At least one
# RRset exists (value-dependent) $packet->push(pre => yxrrset("host.example.com A 10.1.2.3"));
Meaning: At least one
Returns a "Net::DNS::RR" object or "undef" if the object could not be created.
nxrrset
Use this method to add an ``RRset does not exist'' prerequisite to a dynamic update packet.
$packet->push(pre => nxrrset("host.example.com A"));
Meaning: No RRs with the specified name and type can exist.
Returns a "Net::DNS::RR" object or "undef" if the object could not be created.
yxdomain
Use this method to add a ``name is in use'' prerequisite to a dynamic update packet.
$packet->push(pre => yxdomain("host.example.com"));
Meaning: At least one
Returns a "Net::DNS::RR" object or "undef" if the object could not be created.
nxdomain
Use this method to add a ``name is not in use'' prerequisite to a dynamic update packet.
$packet->push(pre => nxdomain("host.example.com"));
Meaning: No
Returns a "Net::DNS::RR" object or "undef" if the object could not be created.
rr_add
Use this method to add RRs to a zone.
$packet->push(update => rr_add("host.example.com A 10.1.2.3"));
Meaning: Add this
Returns a "Net::DNS::RR" object or "undef" if the object could not be created.
rr_del
Use this method to delete RRs from a zone. There are three forms: delete an RRset, delete all RRsets, and delete an
# Delete an RRset. $packet->push(update => rr_del("host.example.com A"));
Meaning: Delete all RRs having the specified name and type.
# Delete all RRsets. $packet->push(update => rr_del("host.example.com"));
Meaning: Delete all RRs having the specified name.
# Delete an RR. $packet->push(update => rr_del("host.example.com A 10.1.2.3"));
Meaning: Delete all RRs having the specified name, type, and data.
Returns a "Net::DNS::RR" object or "undef" if the object could not be created.
Zone Serial Number Management
The Net::DNS module provides auxiliary functions which support policy-driven zone serial numbering regimes.Strictly Sequential
$successor = $soa->serial( SEQUENTIAL );
The existing serial number is incremented modulo 2**32.
Time Encoded
$successor = $soa->serial( UNIXTIME );
The Unix time scale will be used as the basis for zone serial numbering. The serial number will be incremented if the time elapsed since the previous update is less than one second.
Date Encoded
$successor = $soa->serial( YYYYMMDDxx );
The 32 bit value returned by the auxiliary YYYYMMDDxx() function will be used as the base for the date-coded zone serial number. Serial number increments must be limited to 100 per day for the date information to remain useful.
Sorting of RR arrays
As of version 0.55 there is functionality to help you sortrrsort()
use Net::DNS qw(rrsort); @sorted = rrsort( $rrtype, $attribute, @rr_array );
rrsort() selects all RRs from the input array that are of the type defined by the first argument. Those RRs are sorted based on the attribute that is specified as second argument.
There are a number of RRs for which the sorting function is defined in the code. The function can be overidden using the set_rrsort_func() method.
For instance:
@prioritysorted = rrsort( "SRV", "priority", @rr_array );
returns the
If the function does not exist then a numerical sort on the attribute value is performed.
@portsorted = rrsort( "SRV", "port", @rr_array );
If the attribute is not defined then either the default_sort() function or ``canonical sorting'' (as defined by
rrsort() returns a sorted array containing only elements of the specified
rrsort() returns undef when arguments are incorrect.
EXAMPLES
The following examples show how to use the "Net::DNS" modules. See the other manual pages and the demo scripts included with the source code for additional examples.See the "Net::DNS::Update" manual page for an example of performing dynamic updates.
Look up a host's addresses.
use Net::DNS; my $res = Net::DNS::Resolver->new; my $reply = $res->search("host.example.com"); if ($reply) { foreach my $rr ($reply->answer) { next unless $rr->type eq "A"; print $rr->address, "\n"; } } else { warn "query failed: ", $res->errorstring, "\n"; }
Find the nameservers for a domain.
use Net::DNS; my $res = Net::DNS::Resolver->new; my $reply = $res->query("example.com", "NS"); if ($reply) { foreach $rr (grep { $_->type eq 'NS' } $reply->answer) { print $rr->nsdname, "\n"; } } else { warn "query failed: ", $res->errorstring, "\n"; }
Find the MX records for a domain.
use Net::DNS; my $name = "example.com"; my $res = Net::DNS::Resolver->new; my @mx = mx($res, $name); if (@mx) { foreach $rr (@mx) { print $rr->preference, " ", $rr->exchange, "\n"; } } else { warn "Can not find MX records for $name: ", $res->errorstring, "\n"; }
Print a domain's SOA record in zone file format.
use Net::DNS; my $res = Net::DNS::Resolver->new; my $reply = $res->query("example.com", "SOA"); if ($reply) { ($reply->answer)[0]->print; } else { print "query failed: ", $res->errorstring, "\n"; }
Perform a zone transfer and print all the records.
use Net::DNS; my $res = Net::DNS::Resolver->new; $res->nameservers("ns.example.com"); my @zone = $res->axfr("example.com"); foreach $rr (@zone) { $rr->print; }
Perform a background query for the answer.
use Net::DNS; my $res = Net::DNS::Resolver->new; my $socket = $res->bgsend("host.example.com"); until ($res->bgisready($socket)) { # do some work here while waiting for the answer # ...and some more here } my $packet = $res->bgread($socket); $packet->print;
Send a background query using select to detect completion
use Net::DNS; use IO::Select; my $timeout = 5; my $res = Net::DNS::Resolver->new; my $bgsock = $res->bgsend("host.example.com"); my $sel = IO::Select->new($bgsock); # Add more sockets to $sel if desired. my @ready = $sel->can_read($timeout); if (@ready) { foreach my $sock (@ready) { if ($sock == $bgsock) { my $packet = $res->bgread($bgsock); $packet->print; $bgsock = undef; } # Check for the other sockets. $sel->remove($sock); $sock = undef; } } else { warn "timed out after $timeout seconds\n"; }
BUGS
"Net::DNS" is slow.For other items to be fixed, or if you discover a bug in this distribution please use the
COPYRIGHT
Copyright (c)1997-2002 Michael Fuhr.Portions Copyright (c)2002-2004 Chris Reinhardt.
Portions Copyright (c)2005 Olaf Kolkman (
Portions Copyright (c)2006 Olaf Kolkman (NLnet Labs)
Portions Copyright (c)2014 Dick Franks
All rights reserved.
LICENSE
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.AUTHOR INFORMATION
Net::DNS is maintained at NLnet Labs (www.nlnetlabs.nl) by Olaf Kolkman.Between 2002 and 2004 Net::DNS was maintained by Chris Reinhardt.
Net::DNS was created by Michael Fuhr.
For more information see:
www.net-dns.org
Stay tuned and syndicate:
www.net-dns.org/blog