Script to sort Cisco configs

Hi everyone :),

I need sort Cisco configs to report but i cannot do the script to made this:

#### INPUT #####

 
config-register 0x2102
version 12.2
!
hostname Router
!
interface Ethernet0
description Red LAN
ip address 192.168.1.1 255.255.255.0
no cdp enable
!
interface Serial0
description Red WAN
ip address 172.16.1.1 255.255.255.0
!
interface loopback0
shutdown
!
line con 0
exec-timeout 20 0
stopbits 1
!
line vty 0 4
exec-timeout 20 0
transport preferred none
!
end

### OUTPUT ###

config-register 0x2102
version 12.2
hostname Router
interface Ethernet0 description Red LAN
interface Ethernet0 ip address 192.168.1.1 255.255.255.0
interface Ethernet0 no cdp enable
interface Serial0 description Red WAN
interface Serial0 ip address 172.16.1.1 255.255.255.0
interface loopback0 shutdown
line con 0 exec-timeout 20 0
line con 0 stopbits 1
line vty 0 4 exec-timeout 20 0
line vty 0 4 transport preferred none
end

Can someone help me with this?, I was trying with awk, grep, etc but I failed.

Thanks :o

not working

Not perfect but should work

awk 'END{print}/!/{getline;x=$0;next}{if(NR<3){print;next}else{print x,$0}}' file
1 Like

It works fine for the example, thanks a lot, but have an additional issue.

If the configuration has the following statements:

### INPUT ###

!
class-map match-all critical
 match ip dscp af31 
class-map match-all video
 match ip dscp ef 
!

### OUTPUT must be ###

class-map match-all critical match ip dscp af31 
class-map match-all video match ip dscp ef 

### The idea is each parameter is linked with its own parameters ###

I thought something like that "^interface"... "^class-map .. etc.

can you help me with that???...

  1. Try to post all possible keywords.
  2. On your second example you have a space after line-break. Is this a mistake or part of the configuration file?
head -2 input >>ouput
tail +3 input | { read prev
while read a
do
if [[ $prev = "!" ]]
then
        first=$a
else
        if [[ "$a" != "!" ]]
        then
                printf "%s %s\n" "$first" "$a"
        else
                if [[ $prev = $first ]]
                then
                        print "$first"
                fi
        fi
fi
prev=$a
done } >>ouput

euh ...
it wouldn't handle the case

!
class-map match-all critical
 match ip dscp af31 
class-map match-all video
 match ip dscp ef 
!

but only

!
class-map match-all critical
 match ip dscp af31 
!
class-map match-all video
 match ip dscp ef 
!

This is a complete case:

#### INPUT #####

!RANCID-CONTENT-TYPE: cisco
!
!
! model 2600
!
!
config-register 0x2102
version 12.2
service timestamps debug datetime msec localtime
service timestamps log datetime msec localtime
!
hostname Router
!
boot-start-marker
boot-end-marker
!
logging buffered 64000
logging rate-limit 10
logging console critical
!
aaa new-model
!
!
aaa session-id common
ip subnet-zero
ip source-route
!
!
ip cef
ip name-server 172.16.1.1
ip name-server 172.16.1.2
!
!
class-map match-any video
  match  dscp ef 
  match ip precedence 5 
class-map match-any critical
  match  dscp af31 
!
policy-map qos1
  class video
    priority percent 75
  class critical
    bandwidth percent 23
policy-map qos2
  class video
    police 10000 8000 16000 conform-action transmit exceed-action drop violate-action drop
!
!
interface Loopback0
 ip address 1.1.1.1 255.255.255.255
 no ip redirects
!
interface FastEthernet0/1
 description Red WAN
 ip address 192.168.1.1 255.255.255.252
 no ip redirects
 no ip proxy-arp
 no cdp enable
 service-policy output qos1
!
interface FastEthernet0/2
 description Red LAN
 ip address 172.16.1.1 255.255.255.252
 no ip redirects
 no ip proxy-arp
 no ip mroute-cache
 no cdp enable
 service-policy output qos1
!
interface FastEthernet0/2
 shutdown
!
!
router bgp 65000
 no synchronization
 bgp always-compare-med
 bgp log-neighbor-changes
 bgp deterministic-med
 redistribute connected route-map CONNECTED
 redistribute static route-map STATIC
 neighbor 172.16.1.2 remote-as 65001
 neighbor 172.16.1.2 description PRUEBA
 neighbor 172.16.1.2 default-originate
 neighbor 172.16.1.6 remote-as 65001
 neighbor 172.16.1.6 description PRUEBA
 neighbor 172.16.1.6 default-originate
 no auto-summary
 !
 address-family vpnv4
  neighbor 172.16.1.2 activate
  neighbor 172.16.1.2 send-community both
  neighbor 172.16.1.6 activate
  neighbor 172.16.1.6 send-community both
 exit-address-family
!
ip classless
ip route 10.0.0.0 255.255.255.0 192.168.1.1
ip route 172.16.2.0 255.255.255.0 172.16.1.2
!
ip access-list 10 permit any
!
route-map test permit 10
 match address 10
!
route-map test permit 20
 match address 45
!
snmp-server community test
snmp-server trap-source Loopback0
!
control-plane
!
banner motd ^CC
# EQUIPO PRIVADO #
^C
!
line con 0
 exec-timeout 20 0
line aux 0
 exec-timeout 20 0
 transport input telnet
 stopbits 1
line vty 0 4
 exec-timeout 20 0
 transport preferred none
 transport input telnet ssh
!
ntp update-calendar
ntp server 10.1.1.1
end
 
##### EXPECTED OUTPUT ####
 
!RANCID-CONTENT-TYPE: cisco
!
!
! model 2600
!
!
config-register 0x2102
version 12.2
service timestamps debug datetime msec localtime
service timestamps log datetime msec localtime
hostname Router
boot-start-marker
boot-end-marker
logging buffered 64000
logging rate-limit 10
logging console critical
aaa new-model
aaa session-id common
ip subnet-zero
ip source-route
ip cef
ip name-server 172.16.1.1
ip name-server 172.16.1.2
class-map match-any video match  dscp ef 
class-map match-any video match ip precedence 5 
class-map match-any critical match  dscp af31 
policy-map qos1 class video
policy-map qos1 class video priority percent 75
policy-map qos1 class critical
policy-map qos1 class critical bandwidth percent 23
policy-map qos2 class video
policy-map qos2 class video police 10000 8000 16000 conform-action transmit exceed-action drop violate-action drop
interface Loopback0 ip address 1.1.1.1 255.255.255.255
interface Loopback0 no ip redirects
interface FastEthernet0/1 description Red WAN
interface FastEthernet0/1 ip address 192.168.1.1 255.255.255.252
interface FastEthernet0/1 no ip redirects
interface FastEthernet0/1 no ip proxy-arp
interface FastEthernet0/1 no cdp enable
interface FastEthernet0/1 service-policy output qos1
interface FastEthernet0/2 description Red LAN
interface FastEthernet0/2 ip address 172.16.1.1 255.255.255.252
interface FastEthernet0/2 no ip redirects
interface FastEthernet0/2 no ip proxy-arp
interface FastEthernet0/2 no ip mroute-cache
interface FastEthernet0/2 no cdp enable
interface FastEthernet0/2 service-policy output qos1
interface FastEthernet0/2 shutdown
router bgp 65000 no synchronization
router bgp 65000 bgp always-compare-med
router bgp 65000 bgp log-neighbor-changes
router bgp 65000 bgp deterministic-med
router bgp 65000 redistribute connected route-map CONNECTED
router bgp 65000 redistribute static route-map STATIC
router bgp 65000 neighbor 172.16.1.2 remote-as 65001
router bgp 65000 neighbor 172.16.1.2 description PRUEBA
router bgp 65000 neighbor 172.16.1.2 default-originate
router bgp 65000 neighbor 172.16.1.6 remote-as 65001
router bgp 65000 neighbor 172.16.1.6 description PRUEBA
router bgp 65000 neighbor 172.16.1.6 default-originate
router bgp 65000 no auto-summary
router bgp 65000 address-family vpnv4
router bgp 65000 neighbor 172.16.1.2 activate
router bgp 65000 neighbor 172.16.1.2 send-community both
router bgp 65000 neighbor 172.16.1.6 activate
router bgp 65000 neighbor 172.16.1.6 send-community both
router bgp 65000 exit-address-family
ip classless
ip route 10.0.0.0 255.255.255.0 192.168.1.1
ip route 172.16.2.0 255.255.255.0 172.16.1.2
ip access-list 10 permit any
route-map test permit 10 match address 10
route-map test permit 20 match address 45
snmp-server community test
snmp-server trap-source Loopback0
control-plane
banner motd ^CC
# EQUIPO PRIVADO #
^C
line con 0 exec-timeout 20 0
line aux 0 exec-timeout 20 0
line aux 0 transport input telnet
line aux 0 stopbits 1
line vty 0 4 exec-timeout 20 0
line vty 0 4 transport preferred none
line vty 0 4 transport input telnet ssh
ntp update-calendar
ntp server 10.1.1.1
end

-----------------------------------------------------------

Thanks for you help and for your time :slight_smile:

---------- Post updated 10-28-10 at 08:11 AM ---------- Previous update was 10-27-10 at 04:16 PM ----------

This is a near solution,,,,

awk '($1 != "!"){print $0}' router | awk 'END{print}/!/;{
if (/^class-map/ || /^policy-map/ || /^interface/ || /^router/ ||
/^route-map/ || /^ip classless/ || /^ip route/ || /^snmp/ ||
/^banner/ || /^line/ || /^end/){x=$0;next}else{print x,$0}}'

Its not perfet, but work.

---------- Post updated at 11:15 AM ---------- Previous update was at 08:11 AM ----------

This is a final solution,,, if someone has a best please shared.

################################################

awk '($1 != "!"){print $0}' router | awk '{if (/^!RANCID/ ||
/^config/ || /^version/ || /^service/ || /^hostname/ || /^boot/ ||
 /^logging/ || /^aaa/ || /^ip/ || /^snmp/ || /^control/ || /^ntp/ || 
/^end/){print;next}} {if (/^class-map/ || /^policy-map/ || /^interface/ ||
 /^router/ || /^route-map/ || /^ip classless/ || /^ip route/ || /^snmp/ || 
/^banner/ || /^line/){x=$0;next}else{print x,$0}}'

#################################################

Special thanks to Danmero,