]> git.donarmstrong.com Git - bin.git/blob - update_dns_serial
* add missing semicolon
[bin.git] / update_dns_serial
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use IO::File;
7 use POSIX qw(strftime);
8
9 for my $file (@ARGV) {
10     my $fh = IO::File->new($file,'r');
11     my $fh_new = IO::File->new($file.".new",'w');
12     local $/;
13     my $fh_contents = <$fh>;
14     # rip out old serial
15     my ($old_serial) = $fh_contents =~ /IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*(\d+)/;
16     my ($ymd,$iter) = $old_serial =~ /(\d{4}\d{2}\d{2})(\d{2})/;
17     my $new_serial = strftime("%Y%m%d",gmtime())."01";
18     if ($ymd eq strftime("%Y%m%d",gmtime())) {
19         $new_serial = $ymd.($iter+1);
20     }
21     $fh_contents =~ s/(IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*)(\d+)/${1}${new_serial}/;
22     print {$fh_new} $fh_contents;
23     close $fh_new;
24     close $fh;
25     rename($file,"${file}.bak") or die "Unable to rename $file to ${file}.bak: $!";
26     rename("${file}.new",$file) or die "Unable to rename ${file}.new to ${file}: $!";
27     unlink("${file}.bak") or die "Unable to unlink ${file}.bak: $!";
28 }