#!/usr/bin/perl use strict; use warnings; use Martnet::DDNS; use File::Temp qw/tempfile/; my $ddns = Martnet::DDNS->new(); my @vh = $ddns->get(); my @all = parse_slavefile("/etc/bind/martnet.slave.zones.9"); # For each virtual host, see if we've got it already my $changecount = 0; foreach my $i (@vh) { unless (contains_zone($i, @all)) { # If we find any differences, we rewrite the file do_rewrite(@vh); last; } } exit 0; sub parse_slavefile { my ($f) = @_; my @ret; open(my $fh, $f) || die "Can't open $f: $!"; while (<$fh>) { if (/^zone\s+\"([^\"]+)\"\s+\{/) { push ( @ret, { zone => $1 } ); } } return @ret; } sub do_rewrite { my (@vh) = @_; my ($fh, $path) = tempfile(); foreach my $i (sort {$a->{zone} cmp $b->{zone}} @vh) { print $fh "zone \"$i->{zone}\" { type slave; file \"/var/cache/bind/db.$i->{zone}\"; masters { $i->{master}; }; };\n"; } close $fh; print "Installing new slave host list\n"; system("install -o bind -g bind $path /etc/bind/martnet.slave.zones.9"); print "Reloading DNS files\n"; system("rndc reload"); } sub contains_zone { my ($zone, @zl) = @_; foreach my $i (@zl) { return 1 if ($i->{zone} eq $zone); } return 0; }