perlquestion
brianjb
<p>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?
</p>
<p>
This is sample named.conf:
</p>
<code>
###########################################
# 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";
};
</code>
<p>
This is listing of zonefiles in directory:
</p>
<code>
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
</code>
<p>
This is the script so far:
</p>
<code>
#!/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 an 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";
}
</code>
<p>
This is the output when I run it:
</p>
<code>
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
</code>
<p>
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.
</p>