http://qs1969.pair.com?node_id=327266

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

hello,
I wanna replace the TTL for the DNS zone files, before i do that i wanna backup the files to zonefile.bak then replace zone file. I am aware, that just tar the files and replace with replace command. But i would like to do it perl way, since i am earning perl. I havent look the perl command line option, it would be nice if someone can show me code which use the "use strict"

Thanks in advance Fromt he chatbox

Replies are listed 'Best First'.
Re: How to replace the DNS zone file TTL for 300+ zone files
by arden (Curate) on Feb 07, 2004 at 07:57 UTC
    xerophyte, this is a great place to start with learning perl. The tasks you wish to accomplish can be written (in a well structured manner, I know somebody will think of a one-liner method) in less than two dozen lines. The functions you need to use are very common ones which are well documented in ANY perl book (open, if, s///, system, print; to name a few).

        However, I don't think anyone on here wants to write your program for you. First, you won't learn nearly as much if we write it for you. Second, only you know the specifics of your requirements. The desired question is one which includes at least a snippet of the code you have tried and any errors it produced. Most of the monks here are happy to help you along, but we do require that you put some real effort into solving the problem.

    Finally, please take a shot at this yourself and post any problems you have then. When you re-post, please read this and specifically this first.

Re: How to replace the DNS zone file TTL for 300+ zone files
by Roger (Parson) on Feb 07, 2004 at 10:37 UTC
    Learn Perl. Learn to use CPAN. You can roll your own codes, but most likely there's someone else that has done it already.

    use strict; use warnings; use IO::File; use DNS::ZoneParse; my @zonefiles = </path/to/zonefile/*>; foreach my $zonefile (@zonefiles) { my $z = DNS::ZoneParse->new($zonefile); # change ttl in all MX records, for example my $mx = $z->mx; $_->{ttl} = 'foo bar' for (@$mx); # update ttl $z->new_serial(); # write the new zone file to disk my $zf = new IO::File "$zonefile", "w" or die "Error writing zonefile: $!"; print $zf $z->output(); }

Re: How to replace the DNS zone file TTL for 300+ zone files
by z3d (Scribe) on Feb 07, 2004 at 13:17 UTC
    If you're interested in a "fast" way, you may also want to at least give a glance to the -pi switch for calling the interpeter. Without giving away the plot, this will let you (if properly called ;) ) edit a series of files in place and create backups as you go. Sorta. (wiser monks will disagree on my verbage here, I'm sure).



    "I have never written bad code. There are merely unanticipated features."
Re: How to replace the DNS zone file TTL for 300+ zone files
by grinder (Bishop) on Feb 07, 2004 at 08:51 UTC
    it would be nice if someone can show me code which use the "use strict"

    Ok, here you go:

    use strict;

    That wasn't too difficult, was it? Here's a question for you now: do you understand what it does?