Bladernr has asked for the wisdom of the Perl Monks concerning the following question:

Ok... here is another, and I beg your forgiveness, Masters, but this lowly one has not partaken of perl for quite some time.

I have the following bit of code, using DNS::ZoneParse. The ultimate goal is to have a script that will read a list of zonefiles, and act on each one. In the act, it will then A: reformat the zone file (many of ours are hand written and NOT in any form of standardized format (they do meet specs for zone files, but are not easy to read), and B, update various parts of the SOA records. I am interested in updating the Refresh, TTL, minimumTTL, retry, etc for each listed domain.

For now, the only thing this does is update the serial, but it DOES successfully parse the original zone file and create a new zone file based on the old.

I can sucessfully ADD A, CNAME, MX and such records, but I dont need that at the moment, and have removed those routines for now. What I need to ask of you, is can you show me a way, or ways to manipulate the HASH that is created from the SOA. The code below reads it in, and serial can be updated easily, but I cant figure out how to change the rest. I tried a push (as shown in the sample) but that is no good, becuase the soa is not read as an array.

and yes, the majority of this did come directly from the docs for DNS::ZoneParse. The basic examples are great, but what I need to do is not really explained in the docs, and I am but a mere babe in perl-land.
####BEGIN CODE######

#!/usr/bin/perl use strict; use DNS::ZoneParse; my $zonefile = DNS::ZoneParse->new("./zondervan.com"); print "Current A Records \n"; my $a_records = $zonefile->a(); foreach my $record (@$a_records) { print "$record->{name} resolves at $record->{host}\n"; } push (@$a_records, { name => 'new', class => 'IN', host => '127.0.0.7', ttl => '' }); $zonefile->new_serial(); my $soa = $zonefile->soa(); print "serial: ", $soa->{serial}, "\n", "origin: ", $soa->{origin}, "\n", "primary: ", $soa->{primary}, "\n", "refresh: ", $soa->{refresh}, "\n", "retry: ", $soa->{retry}, "\n", "ttl: ", $soa->{ttl}, "\n", "minimumTTL: ", $soa->{minimumTTL}, "\n", "email: ", $soa->{email}, "\n", "expire: ", $soa->{expire}, "\n"; push (@$soa, { refresh => '3600', retry => '600', ttl => '68400', mini +mumTTL => '1800'}); open NEWZONE, ">./zonefi.new" or die "error"; print NEWZONE $zonefile->output(); close NEWZONE;

edited by ybiC: s/pre/code/

Replies are listed 'Best First'.
Re: Zone File manipulation
by esh (Pilgrim) on Aug 15, 2003 at 18:53 UTC

    (Ouch! Don't forget that closing > on a </a> when posting to PerlMonks! I am finding it impossible to edit my previous post using Galeon 1.2.7. Contact me if you are part of the dev team and want further details.)

    Here's a slightly more complicated, but better snippet. This only updates the elements of the hash you want to change and leaves the other ones as previously set:

    %$soa = (%$soa, refresh => '3600', retry => '600', ttl => '68400', minimumTTL => '1800');

    -- Eric Hammond

      Or maybe more reliably:
      $soa->{refresh} = 3600; $soa->{retry} = 600; ...
      Anyway, the point is that the soa() method returns a reference to a hash, not an array ref.

      --isotope
Re: Zone File manipulation
by esh (Pilgrim) on Aug 15, 2003 at 18:46 UTC

    You can read up on hashes in perldata

    I think what you are looking for is as simple as:

    %$soa = ( refresh => '3600', retry => '600', ttl => '68400', minimumTT +L => '1800');

    -- Eric Hammond

    Fixed anchor tag - dvergin 2003-08-15