Skip to content

Instantly share code, notes, and snippets.

@tedb
Created May 7, 2010 01:42
Show Gist options
  • Save tedb/392921 to your computer and use it in GitHub Desktop.
Save tedb/392921 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
=pod
slavepush.pl
Author: Ted Behling <ted@tedb.us>
Copyright 2010 Hargray Communications
See license below
For administrators of the BIND DNS server, this script can be used to keep the configurations of slave BIND servers in sync with the master. This will take a BIND configuration file, extract out the "zone" lines, and render a new configuration file suitable for use on a slave BIND server.
Example invocation:
slavepush.pl --sourcefile /var/named/named.conf.domains --masters 1.1.1.1 --targetfile /var/named/named.conf.domains --rndc /usr/sbin/rndc --fileprefix domains/ --ssh root@slave1 --ssh root@slave2
For example, a --sourcefile that contains:
zone "example.com" IN {
type master;
file "example.com";
};
zone "example.net" IN {
type master;
file "example.net";
};
...will push to the servers specified by --ssh a config file that contains:
zone "example.com" IN { type slave; masters 1.1.1.1; file "domains/example.com"; };
zone "example.net" IN { type slave; masters 1.1.1.1; file "domains/example.net"; };
This script just cares about the zone name; the rest of the "master" configuration is discarded.
Developer notes:
1. read file specified by --sourcefile; get list of zones by grepping for "zone" lines;
exclude lines that have comment of /* !NOSLAVEPUSH! */
2. Generate "slave" BIND config file fragment, with the "masters" IP specified by --masters flag
3. SSH to server specified by --ssh flag(s);
4. over SSH, write to file --targetfile + ".candidate"
5. over SSH, "diff -q targetfile targetfile.candidate";
if exit code 1, do "mv targetfile.candidate targetfile; rndc reconfig"; else "rm targetfile.candidate"
=cut
use strict;
use warnings;
use Getopt::Long;
# Config params are global variables
our ($sourcefile, $masters, $targetfile, $fileprefix, $rndc, @ssh) = ('', '', '', '', 'rndc', ());
GetOptions("sourcefile=s" => \$sourcefile,
"masters=s" => \$masters,
"targetfile=s" => \$targetfile,
"fileprefix=s" => \$fileprefix,
"rndc=s" => \$rndc,
"ssh=s" => \@ssh,
);
#print "sourcefile $sourcefile masters $masters targetfile $targetfile fileprefix $fileprefix ssh @ssh\n";
sub target_contents {
open(my $source, '<', $sourcefile) or die $!;
my $target_contents = '';
while(<$source>) {
# e.g. zone "example.com" IN {
next unless /^\s*zone "([^"]+)"/ && ! /!NOSLAVEPUSH!/;
my $zone = $1;
my $masters_config = $masters ? "masters { $masters; };" : '';
$target_contents .= "zone \"$zone\" IN { type slave; $masters_config file \"$fileprefix$zone\"; };\n";
}
close $source;
return $target_contents;
}
sub push_target_to_ssh {
my ($ssh, $target_contents) = (shift, shift);
my $targetfile_candidate = $targetfile . '.candidate';
my $scp_command = "ssh $ssh 'cat > $targetfile_candidate'";
print "Writing tempfile over SSH...\n";
print "$scp_command\n";
open(my $sshwrite, '|-', $scp_command) or die "can't ssh to write file: $!";
print $sshwrite $target_contents;
close $sshwrite;
# diff will return exit value of 0 if unchanged, 1 if changed
system "ssh", $ssh, "diff -q $targetfile $targetfile_candidate";
my $diff_exit_value = $? >> 8;
if ($diff_exit_value) {
print "New slave config file is different; overwriting old file and doing $rndc reconfig...\n";
system "ssh", $ssh, "mv '$targetfile_candidate' '$targetfile' && $rndc reconfig";
} else {
print "New slave config file is unchanged; deleting tempfile...\n";
system "ssh", $ssh, "rm '$targetfile_candidate'";
}
}
print "Generating slave config file...\n";
my $target_contents = target_contents();
for my $ssh (@ssh) {
push_target_to_ssh($ssh, $target_contents);
}
print "Done.\n";
=pod
The MIT License
Copyright (c) 2010 Hargray Communications
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment