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 6236293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:49:41+00:00 2026-05-24T10:49:41+00:00

Below is the code block that i am currently having issues with. some background,

  • 0

Below is the code block that i am currently having issues with.

some background, I am writing a script that will check our network configs, backed up via rancid, for a set of default/need configuration, and outputting any errors to an email.

It works by reading in a file (file name stored as $configs), i then use regex expresions to check for the existence/non existence of a line of config.

The bellow code block should do the following.

OPen the file handle, then whilst the file is open, locate any lines between the following two pieces of Regex interface GigabitEthernet[1-9]/[0-48] and !

It should then check the lines between the two regex for the existence of the lines

logging event link-status
logging event spanning-tree status
spanning-tree portfast
spanning-tree bpduguard enable

If any of the above lines are not between the two regex d eliminators, then it should push the corresponding error message into the array for processing and eventual emailing later.

Whilst the code block below seems to work (sortof) I end up with duplicate entries, which im sure has something to do with the push statements being inside the while statement..

Normally i would do something like this

  while (<FH>){
  if (/(interface GigabitEthernet[1-9]\/[0-48])/../!/){
  $text = $1;
  if ($_ !~ /logging event link-status/){
  $loggingconfigured++
  }
  }
  if ($loggingconfigured++ == 0) {
  push (@PortChecksIOS, "$configs port $text does not have logging event link-status    set<br>")
  }

But I lose the ability to use $text has it becomes unset outside the while loop.

Below is the code block as it currently stands, any suggestions are welcomed. But please go easy im new to this perl stuff….

sub processPortChecksIOS{
local ($fulldir, $configs, @PortChecksIOS) = @_;
open FH, "$fulldir/$configs" or die $!;


while (<FH>){
if (/(interface GigabitEthernet[1-9]\/[0-48])/../!/){
$text = $1;
        if ($_ =~ /switchport mode access/ && $_ !~ /shutdown/){
            if ($_ !~ /logging event link-status/){
                push (@PortChecksIOS, "$configs port $text does not have logging event link-status set<br>")
            }
            if ($_ !~ /logging event spanning-tree status/) {
                push (@PortChecksIOS, "$configs port $text does not have logging event spanning-tree status set<br>")
            }
            if ($_ !~ /spanning-tree portfast/){
                push (@PortChecksIOS, "$configs port $text does not have spanning-tree portfast set<br>")
            }
            if ($_ !~ /spanning-tree bpduguard enable/){
                push (@PortChecksIOS, "$configs port $text does not have spanning-tree bdpuguard enable set<br>")
            }
            }
        }

   }

return @PortChecksIOS;
}

Sample input sw-a-x.x – input file

interface GigabitEthernet1/0/1
description ftp5
switchport access vlan 51
switchport mode access
load-interval 30
spanning-tree portfast
!
interface GigabitEthernet1/0/2
switchport access vlan 51
switchport mode access
load-interval 30
spanning-tree portfast
!

sw-a-z.z – input file

interface GigabitEthernet1/0/1
description ftp5
switchport access vlan 51
switchport mode access
load-interval 30
spanning-tree portfast
logging event link-status
spanning-tree status
spanning-tree bpduguard enable
!
interface GigabitEthernet1/0/2
switchport access vlan 51
switchport mode access
load-interval 30
spanning-tree portfast
!

expected output

sw-a-x.x interface GigabitEthernet1/0/1 does not have logging event link-status set
sw-a-x.x interface GigabitEthernet1/0/1 does not have logging event spanning-tree status
sw-a-x.x interface GigabitEthernet1/0/1 does not have spanning-tree bdpuguard enable set
sw-a-x.x interface GigabitEthernet1/0/2 does not have logging event link-status set
sw-a-x.x interface GigabitEthernet1/0/2 does not have logging event spanning-tree status
sw-a-x.x interface GigabitEthernet1/0/2 does not have spanning-tree bdpuguard enable set
sw-a-z.z interface GigabitEthernet1/0/2 does not have logging event link-status set
sw-a-z.z interface GigabitEthernet1/0/2 does not have logging event spanning-tree status
sw-a-z.z interface GigabitEthernet1/0/2 does not have spanning-tree bdpuguard enable set
  • 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-24T10:49:42+00:00Added an answer on May 24, 2026 at 10:49 am

    First, always use the strict and warnings pragmas at the top of your code.

    Second, always use the three argument version of open, never the two argument version , and use lexical filehandles:

    open my $fh, "<", "$fulldir/$configs"
        or die "could not open '$fulldir/$configs': $!";
    

    Third, local does not do what you think it does, use my to create variables that exist in the current scope.

    Fourth, do you really want to match 0, 1, 2, 3, 4, or 8 in that second character class, or do you want to match the integers 0 through 48? If you want the later, you need to say

    if (/(interface GigabitEthernet[1-9]\/(?:[1-3]?[0-9]|4[0-8]))\b/ .. /!/) {
    

    I would probably write your code like this:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    sub process_port_checks_ios {
        my ($fulldir, $configs) = @_;
    
        open my $fh, "<", "$fulldir/$configs"
            or die "could not open '$fulldir/$configs': $!";
    
        my $range = qr/(?:[1-3]?[0-9]|4[0-8])\b/;
        local $/ = "\n!\n"; #records are separated by ! at the start of a line
        my @messages;
        while (<$fh>) {
            #skip records that aren't interfaces
            next unless
                my ($interface) = m{(interface GigabitEthernet[1-9]/$range/$range)};
    
            my $base = "$configs port $interface does not have";
            unless (/^logging event link-status$/m) {
                push @messages, "$base logging event link-status set";
            }
            unless (/^logging event spanning-tree status$/m) {
                push @messages, "$base logging event spanning-tree status set";
            }
            unless (/^spanning-tree portfast$/m) {
                push @messages, "$base spanning-tree portfast set";
            }
            unless (/^spanning-tree bpduguard enable$/m) {
                push @messages, "$base spanning-tree bdpuguard enable set";
            }
        }
    
        return @messages;
    }
    
    print map { "$_\n" } process_port_checks_ios ".", "data";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below shows a sample that I've used recently to explain the different
i'm having some problems with collission in a small 2D game i'm writing. I'm
I'm struglling with the below code to make the currently hard-coded width of ColumnDefinition
When running the below code a type is never returned, despite there being a
In the below code snippet can i replace char * to const char *
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
In the below code sample, what does {0:X2} mean? This is from the reflection
In the below code, the ListBox gets filled with the names of the colors
I would like to know how to modify the below code to strip =20
When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should

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.