Net::DNS - Perl interface to the DNS resolver
The programmer should be somewhat familiar with the format of a DNS packet and its various sections. See RFC 1035 or DNS and BIND (Albitz & Liu) for details.
Don't assume that RR objects will be of the type you requested --- always check an RR object's type before calling any of its methods.
print Net::DNS->version, "\n";
Returns the version of Net::DNS.
# Use a default resolver -- can't 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 MX records for the specified name; the list will be sorted by preference. Returns an empty list if the query failed or no MX records were found.
This method does not look up A records --- it only performs MX queries.
See ``EXAMPLES'' for a more complete example.
# RRset exists (value-independent)
$update->push(pre => yxrrset("host.example.com A"));
Meaning: At least one RR with the specified name and type must exist.
# RRset exists (value-dependent)
$packet->push(pre => yxrrset("host.example.com A 10.1.2.3"));
Meaning: At least one RR with the specified name and type must exist and must have matching data.
Returns a "Net::DNS::RR" object or "undef" if the object couldn't be created.
$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 couldn't be created.
$packet->push(pre => yxdomain("host.example.com"));
Meaning: At least one RR with the specified name must exist.
Returns a "Net::DNS::RR" object or "undef" if the object couldn't be created.
$packet->push(pre => nxdomain("host.example.com"));
Meaning: No RR with the specified name can exist.
Returns a "Net::DNS::RR" object or "undef" if the object couldn't be created.
$packet->push(update => rr_add("host.example.com A 10.1.2.3"));
Meaning: Add this RR to the zone.
RR objects created by this method should be added to the ``update'' section of a dynamic update packet. The TTL defaults to 86400 seconds (24 hours) if not specified.
Returns a "Net::DNS::RR" object or "undef" if the object couldn't be created.
# 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.
RR objects created by this method should be added to the ``update'' section of a dynamic update packet.
Returns a "Net::DNS::RR" object or "undef" if the object couldn't be created.
See the "Net::DNS::Update" manual page for an example of performing dynamic updates.
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->search("host.example.com");
if ($query) {
foreach my $rr ($query->answer) {
next unless $rr->type eq "A";
print $rr->address, "\n";
}
} else {
warn "query failed: ", $res->errorstring, "\n";
}
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->query("example.com", "NS");
if ($query) {
foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
print $rr->nsdname, "\n";
}
}
else {
warn "query failed: ", $res->errorstring, "\n";
}
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't find MX records for $name: ", $res->errorstring, "\n";
}
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->query("example.com", "SOA");
if ($query) {
($query->answer)[0]->print;
} else {
print "query failed: ", $res->errorstring, "\n";
}
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;
}
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;
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";
}
For other items to be fixed, please see the TODO file included with the source distribution.
Portions Copyright (c) 2002-2003 Chris Reinhardt.
All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
Net::DNS was created by:
Michael Fuhr
mike@fuhr.org
For more information see:
http://www.net-dns.org/
|
Закладки на сайте Проследить за страницей |
Created 1996-2025 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |