Bash script to audit Cisco configuration

1.6k views Asked by At

I'm currently writing a script to generate a report from cisco configuration for audit purposes. Using 'grep' command, I was able to successfully capture the global configurations.

But the challenge is doing it per interface. For example, I want to know which interfaces have these lines 'no ip redirects', 'no ip unreachables', etc. How can I accomplish this in bash?

Thank you in advance!

1

There are 1 answers

0
Jotne On

This can not be done easy with grep, but awk handle this:

cat file
!
interface GigabitEthernet0/13
 description Server_32_main
 spanning-tree portfast
 no ip redirects
!
interface GigabitEthernet0/14
 description Server_32_log
 switchport access vlan 666
 spanning-tree portfast
!
interface GigabitEthernet0/15
 description UPS_20
 spanning-tree portfast
!

As you see, each group is separated by !, so we use that to separate each record.
To get only interface name do like this:

awk -v RS="!" -F"\n" '/no ip redirects/ {print $2}' file
interface GigabitEthernet0/13

To get interface config do:

awk -v RS="!" '/no ip redirects/' file

interface GigabitEthernet0/13
 description Server_32_main
 spanning-tree portfast
 no ip redirects

To get more patterns in one go:

awk -v RS="!" '/no ip redirects/ || /no ip unreachables/' file