brianjb has asked for the wisdom of the Perl Monks concerning the following question:
I would really appreciate any assistance that I can get here. I am fairly new to perl. I am trying to rewrite my shell scripts to perl. Currently I have a shell script (using sed, awk, grep, etc) that gets a list of all of the zone files in a directory and then looks in named.conf for what is expected to be in there. If there is a file in the directory that isn't listed in named.conf, then it emails and lets me know that there is some stale files for me to look at. In the past, I have used Text::Diff module to do a diff. Is there any way to put these into two arrays and do a diff without that module? Looking for feedback on either using a module, or doing it without a module. There is more than one way, right?
This is sample named.conf:
########################################### # BIND 9 name server configuration file ########################################### controls { inet 127.0.0.1 allow { localhost; } keys { "rndc-key"; }; }; zone "test.com" in { type master; file "db.test.com"; notify yes; }; zone "brian.com" in { type master; file "db.brian.com"; notify yes; }; zone "." in { type hint; file "db.cache"; }; zone "0.0.127.in-addr.arpa" in { type master; file "db.127.0.0"; };
This is listing of zonefiles in directory:
computer:zonefiles brian$ ls -al ~/zonefiles total 0 drwxr-xr-x 5 brian brian 170 May 15 14:58 . drwxr-xr-x 11 brian brian 374 May 15 14:59 .. -rw-r--r-- 1 brian brian 0 May 15 14:48 db.127.0.0 -rw-r--r-- 1 brian brian 0 May 15 14:48 db.brian.com -rw-r--r-- 1 brian brian 0 May 15 14:48 db.test.com
This is the script so far:
#!/usr/bin/perl # Putting all of the zonefiles lised in named.conf into an array my $srce = "named.conf.brian"; my $string1 = "db."; open(my $FH, $srce) or die "Failed to open file $srce ($!)"; my @buf = <$FH>; close($FH); my @lines = grep (/$string1/, @buf); #grepping for db. map {s/"//g; } @lines; #removing quotation marks map {s/;//g; } @lines; #removing semicolon #map {s/^.*file.*db.//g; } @lines; #removing file db. map {s/^.*file //g; } @lines; #removing the word file print @lines; # Putting listing of all of the files in ~/zonefiles directory into a +n array # Assigning variable to directory my $directory = "~/zonefiles/"; opendir(D, "$directory") || die "Can't opendir $directory: $!\n"; my @list = readdir(D); closedir(D); foreach my $f (@list) { print "$f\n"; }
This is the output when I run it:
computer:tmp brian$ ./brian.pl db.test.com db.brian.com db.cache db.127.0.0 . .. 9 db.127.0.0 db.brian.com db.test.com
How would I ignore things that I don't want to check? For example, ignore db.cache in named.conf. I also want to ignore "." and ".." in the zone file directory.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare zone files in directory with what is listed in named.conf
by NetWallah (Canon) on May 15, 2013 at 20:06 UTC | |
|
Re: Compare zone files in directory with what is listed in named.conf
by ig (Vicar) on May 15, 2013 at 20:27 UTC | |
|
Re: Compare zone files in directory with what is listed in named.conf
by fisher (Priest) on May 15, 2013 at 20:15 UTC | |
|
Re: Compare zone files in directory with what is listed in named.conf
by Kenosis (Priest) on May 15, 2013 at 22:17 UTC |