Subversion Repositories sysadmin_scripts

Rev

Blame | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;

# cleans domains from a BIND 9 zone file
# assumes a zone definition in the form
# zone "domainname1"
# ...
# zone "domainname2"
# simply empties all ines between zone "domainname1" and zone "domainname2"
# ANYTHING between those lines is deleted. No parsing done. Be warned.
# cat /etc/bind/zonefilename | ./cleanZones.pl domain1 domain2 > outfile

# slurp in our command line parameters
my %domainsToDelete  = map { $_ => 0 } @ARGV;
# if we don't clear ARGV, perl thinks we're passing filenames
# to open for STDIN
@ARGV = ();

# slurp in STDIN, which is assumed to be a zone file
my @zoneFile = <>;
chomp @zoneFile;

# we are going to look for something like this and delete it
# zone "acpbdesign.com" {
#     type slave;
#      masters {  
#                  74.113.60.154;
#               };
#                      file "SEC/acpbdesign.com";
#                                };
                                                
                                                
my $i = 0;
while ( $i < scalar( @zoneFile ) ) {
   # does this line look like the start of a zone entry?
   if ( $zoneFile[$i] =~ m/zone\s+"([^"]+)"/ ) {
      # yes, so get the domain name
      my $domain =  $1;
      # see if it is in our ToDelete list
      if ( defined $domainsToDelete{ $domain } ) {
         # tell user
         print STDERR "deleting domain $domain\n";
         # flag the deletion
         $domainsToDelete{ $domain } = 1;
         # just blank the line
         # TODO: delete the line
         $zoneFile[$i] = '';
         # do the same to all subsequent lines, up until the next zone defintion
         # or eof
         while ( $i < scalar( @zoneFile ) && $zoneFile[$i] !~ m/zone\s+"([^"]+)"/ ) {
            $zoneFile[$i++] = '';
         }
         next;
      }
   }
   $i++;
}

# tell user, on STDERR, any domains we could not find in the zone file
foreach my $domain ( keys %domainsToDelete ) {
   print STDERR "could not find domain $domain\n" unless $domainsToDelete{$domain};
}

# dump the modified zone file.
print join( "\n", @zoneFile ) . "\n";

1;