-2

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!

beatnik
  • 1
  • 1
  • 3
    You should add more details to the question, like what you have tried, what outputs you obtained, how you want the output to be etc – nu11p01n73R Nov 13 '14 at 04:42

1 Answers1

0

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
Jotne
  • 40,548
  • 12
  • 51
  • 55