Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3979260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:08:52+00:00 2026-05-20T05:08:52+00:00

Here is what im trying to achieve. I have a perl script that looks

  • 0

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 logging 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:08:53+00:00Added an answer on May 20, 2026 at 5:08 am
    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";
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to achieve: I have a div that switches between fixed
What I'm trying to achieve here is lets say we have two example URLs:
Here's what I'm trying to do, and failing... I have a File model which
here's what I'm trying to do : On a page, I would have a
Here's what I'm trying to do : I have an entity Task with a
what I am trying to achieve is to have a persistent list of undoable
I'm trying to achieve something like you have in the back end of Wordpress
I'm trying to achieve the following functionality. Within a form I have multiple fields
Here is my question: I have a hybrid chart that uses bars and lines
Programming Student here...trying to work on a project but I'm stuck. The project is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.