I would like to first thank those who originally replied to my first article. I have the script modified so it's pulling all the needed data and I finished my work very fast! There was a lot of new syntaxes that I didn't know and took tutorials on each to learn a little more on them.. I'm on my second audit project which requires me to read a Cisco Router config file, pull out the following values:
Interface
Description
Applied ACL INBOUND (IF ANY)
Applied ACL OUTBOUND (IF ANY)
shutdown flag (Yes/No)
Sample of Text File:
!
interface Serial8/0/2/1:0.304 point-to-point
description Connection to Secret Site X
bandwidth 56
ip address x.x.x.x x.x.x.x
ip access-group ACL_IN in
ip access-group ACL_OUT out
no ip unreachables
frame-relay (omitted code)
!
interface Serial8/0/2/1:0.305 point-to-point
description Connection to Site B
bandwidth 1536
ip address x.x.x.x x.x.x.x
no ip unreachables
frame-relay (omitted code)
!
interface Serial8/0/3/1:0
description Connection to Site A
ip address x.x.x.x x.x.x.x.x
ip access-group ACL123 in
no ip unreachables
!
interface Serial8/0/2/1:0.305 point-to-point
description Connection to Site F
bandwidth 1536
ip address x.x.x.x x.x.x.x
ip access-group ACL123 in
no ip unreachables
frame-relay (omitted code)
shutdown
!
-----------------------------------------
Pseudo-Code
Read the file 'running-config.txt'
LOOP: until the last line of the text file {
find "interface GigabitEthernet" OR "interface Serial" & add to $interface
LOOP: until ! is found {
find "description" and add to $description
find "ip access-group"
if found, add to string
else add "NO ACL" to $ACL1
find "ip access-group"
if found, add to string
else add "NO ACL" to $ACL2
find "shutdown"
if found, add "YES" to $shutdown
else add "NO" to $shutdown
END inner LOOP once ! is found and continue outer LOOP
}
PRINT $interface,", ",$ACL1,", ",$ACL2,", ",$shutdown,", ",$description,", " . "\n";
}
Here is the actual code I have:
#!/usr/bin/perl
use warnings;
use strict;
my $Interface =" ";
my $description = " ";
my $group = 'access-group';
my $group1 = 'access-group';
my $shutdown = " ";
my $input_file = 'runningconfig.txt';
open my $fh, '<', $input_file or die "can't open file:$!";
while (<$fh>) {
if (/interface GigabitEthernet/ or /interface Serial/) {
$Interface = substr $_, ( ( index $_, q{" "} ) + 1 ), $#_;
while ($_ != '!')
{
if (/description/) {
my $description = substr $_, ( ( index $_, q{" "}) + 1 ), $#_;
}
if (/access-group/){
my $group = substr $_, ( ( index $_, q{" "}) + 1 ), $#_;}
if (/access-group/){
my $group1 = substr $_, ( ( index $_, q{" "}) + 1 ), $#_; }
if (/shutdown/){
my $shutdown = substr $_, ( ( index $_, q{" "}) + 1 ), $#_;}
}
}
print "$Interface, $description, $group, $group1, $shutdown.\n";
}
#else { next }
close $fh or die "can't close file:$!";
It displays some wacky stuff and now what I was expecting. Any help would be greatly appreciated.
-Serpterion