Perl Script Help | Readthrough group of files, looking for specific match
Here is what im trying to achieve.
I have a perl script that looks into a group of directly parse the files it finds and locates a specific string. If it finds the specific string, it ignores it, if it finds the first part of the string, but the second part doesnt match, it writes it out to a log file.
The part im stuck with, is how to read the entire file in, and look to see if the string exists in the entire file.
Some background, im trying to write a script that will read cisco rancid files, and looks for the sys logging details. These are stored in the config as such
logging x.x.x.x
where x.x.x.x is the syslog IP address.
Currently, i can read in the file, check it line by line, and see if the file contains logging x.x.x.x, if it does, it ignores it.
If it contains logging y.y.y.y (where y is an IP different to what it should be), it will write out to a log file, that the logging is setup, but incorrectly configured. But i cant for the life of me work out how to get it to read the entire file, and if logging x.x.x.x or even logging y.y.y.y doesnt exist to write out that it is not configured.
script is below
#!/usr/bin/perl
use strict;
use warnings;
#Syslog Checker.... V0.0.1
##Config Items
my $basedir = "/home/srelf/Documents/Projects/perl/Configs";
my @verdir = qw(sw_a);
my $fulldir;
my $configs;
my $loghost = "x.x.x.x";
my $combidir;
use POSIX qw(strftime);
$now_string = strftime "%a%b%e%Y", gmtime;
open FILE, ">>Configchecker.$now_string.txt" or die $!;
print FILE "### Logging Host Settings ###\n";
close FILE;
foreach $combidir (@verdir) {
$fulldir = "$basedir/$combidir";
opendir( DIR, $fulldir );
my @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR;
closedir(DIR);
while ( @files > 0 ) {
$configs = pop @files;
# print "$fulldir/$configs\n"; # used for debug shows current file with full path.
open FH, "$fulldir/$configs" or die $!;
@dataarry = <FH>;
foreach my $line (@dataarry) {
# Start an if statement, the condition of which is
# "If this particular line contains logging + IP address."
if ( $line =~ m/logging \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/i ) {
#then if the line located above contains logg开发者_StackOverflow社区ing and the log host ignore it
if ( $line =~ m/logging $loghost/i ) {
}
# if the above line contains an ip but it is not the correct ip do the below.
elsif ( $line =~
m/logging \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/i )
{
open FILE, ">>Configchecker.$now_string.txt" or die $!;
print FILE "$configs\n";
print FILE
"Logging for this device is set, but pointing at the wrong host: $line\n";
close FILE;
} # End the if condition here.
}
}
} # End the foreach loop here;
open FILE, ">>Configchecker.$now_string.txt" or die $!;
print FILE "### NTP Settings Check ###\n";
close FILE;
}
Thanks in advance for any help.
Im very new to perl, this is my first shot. Steve.
foreach my $configs (@files)
{
my $CONFIGURED=0;
open FH, "$fulldir/$configs" or die $!;
while (<FH>)
{
if ($_ =~ m/logging/)
{
$CONFIGURED++;
}
if ($_ =~ m/logging \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/i and $_ !~ m/logging $loghost/i)
{
print "Logging for this device is set, but pointing at the wrong host: $_\n";
}
}
if ($CONFIGURED == 0)
{
print "NOT CONFIGURED $configs\n";
}
}
精彩评论