Tie::DNS - Tie interface to Net::DNS
This document describes version 0.5 of Tie::DNS, released February 28, 2008
use Tie::DNS; tie(my %dns, 'Tie::DNS'); print "$dns{'foo.bar.com'}\n"; print "$dns{'208.180.41.1'}\n";
Net::DNS is a very complete, extensive and well-written module. It's completeness, however, makes many comman cases uses a bit wordy, code-wise. Tie::DNS is meant to make common DNS operations trivial, and more complex DNS operations easier.
See Above.
Get all of the A records from 'foo.com'. (Sorry foo.com if everyone hits your name server testing this module. :-)
tie (%dns, 'Tie::DNS', {'Domain' => 'foo.com'}); while (my ($name, $ip) = each %dns) { print "$name = $ip\n"; }
This obviously requires that your host has zone transfer privileges with a name server hosting that zone. The zone transfer is initiated with the first each, keys or values operation. The tie operation does a SOA query to find the name server for the cited zone.
Pass the configuration parameter of 'multiple' to any Perl true value, and all FETCH values from Tie::DNS will be an array reference of records.
tie (my %dns, 'Tie::DNS', {'multiple' => 'true'}); my $ip_ref = $dns{'cnn.com'}; foreach (@{$ip_ref}) { print "Address: $_\n"; }
Pass the configuration parameter of 'type' to one of the Net::DNS supported record types causes all FETCHes to get records of that type.
tie (%dns, 'Tie::DNS', { 'multiple' => 'true', 'type' => 'SOA'}); my $ip_ref = $dns{'cnn.com'}; foreach (@{$ip_ref}) { print "primary nameserver: $_\n"; }
Here are the most popular types supported:
CNAME - Returns the records canonical name. A - Returns the records address field. TXT - Returns the descriptive text. MX - Returns name of this mail exchange. NS - Returns the domain name of the nameserver. PTR - Returns the domain name associated with this record. SOA - Returns the domain name of the original or nameserver for this zone. (The descriptions are right out of the Net::DNS POD.)
See Net::DNS documentation for further information about these types and a comprehensive list of all available types.
tie (%dns, 'Tie::DNS', {'type' => 'SOA'}); my $dns_ref = $dns{'cnn.com'}; foreach my $field (keys %{$dns_ref}) { print "$field = " . ${$dns_ref}{$field} . "\n"; }
This code fragment will print all of the SOA fields associated with cnn.com.
The argument 'cache' will cause the DNS results to be cached. The default is no caching. The 'cache' argument is passed through to Tie::Cache. If Tie::Cache cannot be loaded, caching will be disabled. Entries whose DNS TTL has expired will be re-queried automatically.
tie (%dns, 'Tie::DNS', { cache => 100 }); print "$dns{'cnn.com'}\n"; print "$dns{'cnn.com'}\n"; ## cached!
tie (%dns, 'Tie::DNS', {'all_fields' => 'true'}); my $dns_ref = $dns{'cnn.com'}; print $dns_ref->{'ttl'}, "\n";
tie (%dns, 'Tie::DNS', {'type' => 'SOA'}); print $dns{'cnn.com'} . "\n"; tied(%dns)->args({'type' => 'A'}); print $dns{'cnn.com'} . "\n";
This code fragment first does an SOA query for cnn.com, and then changes the default mode to A queries, and displays that.
Assign into the hash, key DNS name, value IP address, to add a record to the zone in the domain argument. For instance:
tie (%dns, 'Tie::DNS', { 'domain' => 'realms.lan', 'multiple' => 'true'}); $dns{'food.realms.lan.'} = '131.22.40.1'; foreach (@{$dns{'food'}}) { print " $_\n"; }
Returns the last error, either from Tie::DNS or Net::DNS
Returns the root name server.
Returns the results of a forward lookup.
Returns the results of a reverse lookup.
Change various arguments to the tie on the fly.
This 0.61 release supports the basic functionality of Net::DNS. The 1.0 release will support the following:
Different access methods for forward and reverse lookups.
The 2.0 release will strive to support DNS security options.
Dana M. Diederich <dana@realms.org>
kevin brintnall <kbrint@rufus.net> for Caching patch
in-addr.arpa zone transfers aren't yet supported.
Patches, flames, opinions, enhancement ideas are all welcome.
To install Tie::DNS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::DNS
CPAN shell
perl -MCPAN -e shell install Tie::DNS
For more information on module installation, please visit the detailed CPAN module installation guide.