DNS::ZoneParse - Perl extension for parsing and manipulating DNS Zone Files.
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();
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
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...
Example: my $dnsfile = DNS::ZoneParse->new();
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()
is called internally by the Prepare()
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()
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()
loops through the Resource Records and creates a zone file in $object->{ZoneFile}. It also returns the new zonefile.
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});
None by default. Object-oriented interface.
These are things that I need to do...
Please let me know!
S. Flack : perl@simonflack.com
DNS::ZoneFile