-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincserial.pl
executable file
·94 lines (69 loc) · 2 KB
/
incserial.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/perl
use 5.014;
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
# Options with defaults
my %opts = ( serialfmt => 'increment' );
GetOptions(\%opts,'serialfmt=s','v') || pod2usage;
# main (kind of)
{
my $zonefile = shift;
unless ( defined $zonefile ) {
say "zonefile not specified";
pod2usage;
}
&increment_serial($zonefile);
}
sub increment_serial {
my $zonefile = shift;
return if ($opts{serialfmt} eq 'keep');
say "increment serial";
open(ZONEFILE, '<', $zonefile) || die "open $zonefile failed: $!";
my @zone = <ZONEFILE>;
close ZONEFILE;
my $changed = 0;
for (@zone) {
if ($opts{serialfmt} eq 'increment'
&& s/^(\s*)(\d+)\s+;\s+serial$/sprintf("%s%-11d; serial",$1,$2+1)/e) {
$changed = 1;
last;
}
if ($opts{serialfmt} eq 'unixtime'
&& s/^(\s*)(\d+)\s+;\s+serial$/sprintf("%s%-11d; serial",$1,time)/e) {
$changed = 1;
last;
}
if ($opts{serialfmt} eq 'datestr'
&& s/^(\s*)(\d{8})(\d{2})\s+;\s+serial$/sprintf("%s%-11d; serial",$1,&dateserial($2,$3))/e) {
$changed = 1;
last;
}
}
return unless $changed;
open(ZONEFILE, '>', $zonefile) || die "open $zonefile failed: $!";
print ZONEFILE @zone;
close ZONEFILE;
}
sub dateserial {
my ($prevdate,$seq) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $curdate = sprintf("%04d%02d%02d",$year + 1900,$mon+1,$mday);
return $prevdate . sprintf("%02d",$seq+1) if ($prevdate eq $curdate);
return $curdate . "01";
}
=head1 NAME
B<incserial> - Increment serial in a zonefile
=head1 SYNOPSIS
B<incserial>
S<[ B<--serialfmt> I<keep|increment|unixtime|datestr> ]>
S<zonefile>
=head1 OPTIONS
=over
=item B<--serialfmt> I<keep|increment|unixtime|datestr>
Type of serial to generate. Default is increment.
=back
=head1 DESCRIPTION
B<incserial> increments serial in a DNS zonefile.
=cut