]> git.donarmstrong.com Git - bin.git/commitdiff
add update_dns_serial command
authorDon Armstrong <don@donarmstrong.com>
Sun, 8 Mar 2009 00:23:16 +0000 (00:23 +0000)
committerDon Armstrong <don@donarmstrong.com>
Sun, 8 Mar 2009 00:23:16 +0000 (00:23 +0000)
update_dns_serial [new file with mode: 0755]

diff --git a/update_dns_serial b/update_dns_serial
new file mode 100755 (executable)
index 0000000..72edce2
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use IO::File;
+use POSIX qw(strftime);
+
+for my $file (@ARGV) {
+    my $fh = IO::File->new($file,'r');
+    my $fh_new = IO::File->new($file.".new",'w');
+    local $/;
+    my $fh_contents = <$fh>;
+    # rip out old serial
+    my ($old_serial) = $fh_contents =~ /IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*(\d+)/;
+    my ($ymd,$iter) = $old_serial =~ /(\d{4}\d{2}\d{2})(\d{2})/;
+    my $new_serial = strftime("%Y%m%d",gmtime)."01"
+    if ($ymd == strftime("%Y%m%d",gmtime)) {
+       $new_serial = $ymd.($iter+1);
+    }
+    $fh_contents =~ s/(IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*)(\d+)/${1}${new_serial}/;
+    print {$fh_new} $fh_contents;
+    close $fh_new;
+    close $fh;
+    rename($file,"${file}.bak") or die "Unable to rename $file to ${file}.bak: $!";
+    rename("${file}.new",$file) or die "Unable to rename ${file}.new to ${file}: $!";
+    unlink("${file}.bak") or die "Unable to unlink ${file}.bak: $!";
+}