NAME
DNS::ZoneParse - Perl extension for parsing and manipulating DNS Zone Files.
SYNOPSIS
use DNS::ZoneParse;
my $dnsfile = DNS::ZoneParse->new();
$dnsfile->Prepare("/path/to/dns/zonefile.db");
print $dnsfile->{Zone}->{SOA}->{serial};
$dnsfile->newSerial();
print $dnsfile->{Zone}->{SOA}->{serial};
print $dnsfile->PrintZone();
INSTALLATION
perl Makefile.PL
make
make test
make install
Win32 users substitute "make" with "nmake" or equivalent. nmake is available at http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe
DESCRIPTION
This module will parse a Zone File and put all the Resource Records (RRs) into an anonymous hash structure. At the moment, the following types of RRs are supported: SOA, NS, MX, A, CNAME, TXT, PTR. It could be useful for maintaining DNS zones, or for transferring DNS zones to other servers. If you want to generate an XML-friendly version of your zone files, it is easy to use XML::Simple with this module once you have parsed the zonefile.
The Prepare method scans the DNS zonefile - removes comments and seperates the file into it's constituent records. It then parses each record and stores the objects in the $object->{Zone} hash. Using Data::Dumper on that object will give you a better idea of what this looks like than I can describe.
You can access the objects in the $object->{Zone} hash to add\remove\modify RRs directly, and then you can call $object->PrintZone(), and it will return and create a new Zone File in the $object->{ZoneFile} string.
I will update this documentation - it's pretty sparse at the moment, but many more features coming...
METHODS
- new
-
This creates the DNS::ZoneParse Object
Example: my $dnsfile = DNS::ZoneParse->new();
- Prepare
-
Prepare()
will do some preliminary checks and then parse the supplied DNS Zone File. You can pass it the text content from the DNS Zone File as a reference or the path to a filename.Examples:
$dnsfile->Prepare("/path/to/zonefile.db");
or
my $zonefile; open (Zone, "/path/to/zonefile.db"); while (<Zone>) { $zonefile .= $_ } close (Zone); $dnsfile->Prepare(\$zonefile);
- Parse
-
Parse()
is called internally by thePrepare()
method. You can call it independently. It takes no arguments. All that is required is that $object->{ZoneFile} (string) contains a valid DNS Zone File. - newSerial
-
newSerial()
incriments the Zone serial number. You can pass a positive number to add to the current serial number or it will default to 1.Examples:
$dnsfile->newSerial(); # adds 1 to the original serial number $dnsfile->newSerial(50); # adds 50 to the original serial number $dnsfile->newSerial(-50); # adds 1 to the original serial number
- PrintZone
-
PrintZone()
loops through the Resource Records and creates a zone file in $object->{ZoneFile}. It also returns the new zonefile.
EXAMPLES
This script will print the A records in a zone file, add a new A record for the name "new" and then return the zone file.
use strict;
use DNS::ZoneParse;
my $dnsfile = DNS::ZoneParse->new();
$dnsfile->Prepare("/path/to/zonefile.db");
print "Current A Records\n";
foreach my $a (@{$dnsfile->{Zone}->{A}}) {
print "$a->{name} resolves at $a->{host}\n";
}
push (@{$dnsfile->{Zone}->{A}}, { name => 'new', class => 'IN', host => '127.0.0.1', ttl => '' });
$dnsfile->newSerial();
my $newfile = $dnsfile->PrintZone();
This script will convert a DNS Zonefile to an XML file using XML::Simple.
use strict;
use DNS::ZoneParse;
use XML::Simple;
my $dnsfile = DNS::ZoneParse->new();
$dnsfile->Prepare("/path/to/zonefile.db");
my $new_xml = XMLout($dnsfile->{Zone}, noattr => 1, suppressempty => 1, rootname => $dnsfile->{Zone}->{SOA}->{serial});
EXPORT
None by default. Object-oriented interface.
TO DO
These are things that I need to do...
- IPv6 compatability
-
There is already space for the AAAA records, but I need to read the rest of the RFCs before I finish this part.
- More intelligent serial number generation.
-
Possibly add the option of date-based updates to serial number
- Cleaner parsing
-
The parsing here does work on the tested systems, but there may be cleaner ways of doing it.
- Better documentation
-
Come on, you know this is hopeless!
BUGS & REQUESTS
Please let me know!
AUTHOR
S. Flack : perl@simonflack.com
SEE ALSO
DNS::ZoneFile
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 323:
=over should be: '=over' or '=over positive_number'