DNS::ZoneParse - Perl extension for parsing and manipulating DNS Zone Files.


        use DNS::ZoneParse;
        my $dnsfile = DNS::ZoneParse->new();
        print $dnsfile->{Zone}->{SOA}->{serial};
        print $dnsfile->{Zone}->{SOA}->{serial};

        print $dnsfile->PrintZone();


   perl Makefile.PL
   make test
   make install

Win32 users substitute ``make'' with ``nmake'' or equivalent. nmake is available at


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...


This creates the DNS::ZoneParse Object

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.




    my $zonefile;
    open (Zone, "/path/to/zonefile.db");
    while (<Zone>) { $zonefile .= $_ }
    close (Zone);

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.


    $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();

    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 => '', ttl => '' });
    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();
    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...

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!


Please let me know!


S. Flack :