]> git.donarmstrong.com Git - bin.git/blob - update_dns_serial
add reset usb bus command
[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') or die "Unable to open $file for reading: $!";
11     my $fh_new = IO::File->new($file.".new",'w') or die "Unable to open ${file}.new for writing: $!";
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     if (not defined $old_serial) {
17         print STDERR "problem dealing with $file\n";
18         unlink("${file}.new") or die "Unable to unlink ${file}.new: $!";
19         next;
20     }
21     my ($ymd,$iter) = $old_serial =~ /(\d{4}\d{2}\d{2})(\d{1,2})/;
22     my $new_serial = strftime("%Y%m%d",gmtime())."01";
23     if ($ymd eq strftime("%Y%m%d",gmtime())) {
24         $new_serial = $ymd.sprintf('%02d',$iter+1);
25     }
26     $fh_contents =~ s/(IN\s+SOA\s+\S+\s+\S+\s*(?:\s*\;.+\n)*\((?:\s*\;.+\n)*\s*)(\d+)/${1}${new_serial}/;
27     print {$fh_new} $fh_contents;
28     close $fh_new;
29     close $fh;
30     rename($file,"${file}.bak") or die "Unable to rename $file to ${file}.bak: $!";
31     rename("${file}.new",$file) or die "Unable to rename ${file}.new to ${file}: $!";
32     unlink("${file}.bak") or die "Unable to unlink ${file}.bak: $!";
33 }