Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
77 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
 
6
# cleans domains from a BIND 9 zone file
7
# assumes a zone definition in the form
8
# zone "domainname1"
9
# ...
10
# zone "domainname2"
11
# simply empties all ines between zone "domainname1" and zone "domainname2"
12
# ANYTHING between those lines is deleted. No parsing done. Be warned.
13
# cat /etc/bind/zonefilename | ./cleanZones.pl domain1 domain2 > outfile
14
 
15
# slurp in our command line parameters
16
my %domainsToDelete  = map { $_ => 0 } @ARGV;
17
# if we don't clear ARGV, perl thinks we're passing filenames
18
# to open for STDIN
19
@ARGV = ();
20
 
21
# slurp in STDIN, which is assumed to be a zone file
22
my @zoneFile = <>;
23
chomp @zoneFile;
24
 
25
# we are going to look for something like this and delete it
26
# zone "acpbdesign.com" {
27
#     type slave;
28
#      masters {  
29
#                  74.113.60.154;
30
#               };
31
#                      file "SEC/acpbdesign.com";
32
#                                };
33
 
34
 
35
my $i = 0;
36
while ( $i < scalar( @zoneFile ) ) {
37
   # does this line look like the start of a zone entry?
38
   if ( $zoneFile[$i] =~ m/zone\s+"([^"]+)"/ ) {
39
      # yes, so get the domain name
40
      my $domain =  $1;
41
      # see if it is in our ToDelete list
42
      if ( defined $domainsToDelete{ $domain } ) {
43
         # tell user
44
         print STDERR "deleting domain $domain\n";
45
         # flag the deletion
46
         $domainsToDelete{ $domain } = 1;
47
         # just blank the line
48
         # TODO: delete the line
49
         $zoneFile[$i] = '';
50
         # do the same to all subsequent lines, up until the next zone defintion
51
         # or eof
52
         while ( $i < scalar( @zoneFile ) && $zoneFile[$i] !~ m/zone\s+"([^"]+)"/ ) {
53
            $zoneFile[$i++] = '';
54
         }
55
         next;
56
      }
57
   }
58
   $i++;
59
}
60
 
61
# tell user, on STDERR, any domains we could not find in the zone file
62
foreach my $domain ( keys %domainsToDelete ) {
63
   print STDERR "could not find domain $domain\n" unless $domainsToDelete{$domain};
64
}
65
 
66
# dump the modified zone file.
67
print join( "\n", @zoneFile ) . "\n";
68
 
69
1;