Script to sort Cisco configs

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring Script to sort Cisco configs
# 1  
Old 10-27-2010
Script to sort Cisco configs

Hi everyone Smilie,

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

#### INPUT #####
Code:
 
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 ###
Code:
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 Smilie

Last edited by Scott; 10-27-2010 at 03:42 PM.. Reason: Please use code tags
# 2  
Old 10-27-2010
Code:
not working

# 3  
Old 10-27-2010
Not perfect but should work
Code:
awk 'END{print}/!/{getline;x=$0;next}{if(NR<3){print;next}else{print x,$0}}' file

This User Gave Thanks to danmero For This Post:
# 4  
Old 10-27-2010
It works fine for the example, thanks a lot, but have an additional issue.

If the configuration has the following statements:

### INPUT ###
Code:
!
class-map match-all critical
 match ip dscp af31 
class-map match-all video
 match ip dscp ef 
!

### OUTPUT must be ###
Code:
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???...

Last edited by Scott; 10-27-2010 at 03:42 PM..
# 5  
Old 10-27-2010
  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?
# 6  
Old 10-27-2010
Code:
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

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

but only

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


Last edited by ctsgnb; 10-27-2010 at 05:42 PM..
# 7  
Old 10-28-2010
Hammer & Screwdriver

This is a complete case:

#### INPUT #####

Code:
!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 Smilie

Moderator's Comments:
Mod Comment Please use code tags!


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

This is a near solution,,,,

Code:
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.

################################################
Code:
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,

Last edited by Scott; 10-28-2010 at 01:45 PM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. IP Networking

How to stack Cisco 2960-S and Cisco 2960X?

Is there an easy way to stack Cisco 2960-S and Cisco 2960X switches? If you have no idea, follow this: 1. Stacking is not supported on switches running the LAN Lite image. All switches in the stack must be running the LAN Base image. 2. In a mixed stack of Catalyst 2960-X and Catalyst 2960-S... (0 Replies)
Discussion started by: Ayaerlee
0 Replies

2. Shell Programming and Scripting

Expect script to show cisco configs

I know there are better ways to do this. I prefer snmp. I do not have the proper perl modules loaded on the platorm. Snmp isnt loaded on the platform. Telnet is not an option. I need to write an expect script to pull cisco equipment configs. The following code is executed once I gain... (0 Replies)
Discussion started by: popeye
0 Replies

3. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

4. Shell Programming and Scripting

loop through configs

hi All, is there a way in linux to loop thru all variables sourced? i have a set configs like A=100 b=200 c=400 i can add the above lines to a file and source - so that $A will be 100 .. like wise now when i do a cut -c 1-2 file.txt |sort | uniq -c on a file it returns me A 100 B 50 c... (1 Reply)
Discussion started by: arun1401
1 Replies

5. UNIX for Dummies Questions & Answers

VI - in need of configs/scripts

Hi, I'm somewhat new to unix OS and I'm at course for programmers in my country. and in the course we learn unix and how to script in unix. of course we just started and we learned only the very basic, but I'm a really computer freak and I looking for a way to make the course easier on all of... (8 Replies)
Discussion started by: Bonzay0
8 Replies

6. Shell Programming and Scripting

Need help on ssh login script to cisco ios

I'm trying to write a login script to ssh into a cisco switch that will run some command remotely. Similar to this expect script located here: SSH login expect shell script to supply username and password However, that script does not work with cisco ios. Anyway know what the best way to... (1 Reply)
Discussion started by: streetfighter2
1 Replies
Login or Register to Ask a Question