Parsing Cisco Configuration using TCL and bash scripts

Hello All,

its been a while since i blogged the last time, but i was pretty busy and overwhelmed by work. So today we are going to speak about extracting Cisco configurations “switches, routers, ASA, … etc” from the device to you baby Linux machine for manipulation.

In order to do so i wrote 2 scripts

1.  TCL script for exporting or configuring Cisco device

2. Bash script for manipulating the input in pretty complex way

We will start by TCL code which i downloaded part of it from some website and updated it because TCL is pretty cryptic for me 🙂 . What it will do is getting the  vlan brief save it in a log file them get interfaces status and execute a bash my script in order to be parsed.

You could use this script for large scale deployment and you could push configuration through TCL script which i didn’t do because it is almost like entering the password or entering any sh command.

I hope that it will come in handy for some people and education for others 🙂

If you have any question regarding this topic or some other topics please contact me and i will be more than happy to help if i could. Enjoy 🙂

so to simplify the things TCL script will do as follow

  1.  Connect to Cisco device in my case it is a stack
  2.  Pass the pass word then pass enable command and the related password
  3. Start logging and eliminate the CISCO pager since i need the full output without interaction
  4. Issue “sh vlan brief to get vlan id description name and ports, and after writing the results to a file i stop logging
  5. Start logging to a different file , and remove the pager
  6. Execute “sh interface status” to get the interfaces name, vlan id and status
  7. Stop logging and execute the bash script to parser the generated files
  8. when executing the script you need to pass the IP address as an argument
#!/usr/bin/expect -f
#!/bin/bash
set force_conservative 1 ;
# set to 1 to force conservative mode even if;
# script wasn't run conservatively originally

if {$force_conservative} {
 set send_slow {1 .1}
 proc send {ignore arg} {
  sleep .1
  exp_send -s -- $arg }
}
set timeout 3000
log_user 1
set var1 [lindex $argv 0 ]
set var2 [lindex $argv 1]
puts $var1
puts $var2
spawn ssh -1 -l username$var1
match_max 100000

expect "*assword: " {send -- "password1\r"}
#log_user 1
sleep .5
expect ">"
send -- "en\r"
 expect "*assword:" {
 send -- "password2\r"
}
log_file /root/scripts/vlans_per_id
expect "#" {send -- "term len 0\r"}
expect "#" {send -- "sh vlan brief\r"}
expect "#" {send -- "\r"}
sleep .5
log_file

log_file /root/scripts/vlans_per_interface
expect "#" {send -- "term len 0\r"}
expect "#" {send -- "sh interfaces status\r"}
expect "#" {send -- "\r"}
expect "#" {send -- "exit\r"}
expect "#" {send -- "exit\r"}
log_file

exec /bin/bash parser.sh $var1
exit

The bash script will do the following

  1. read the vlans_per_id file and remove the left overs “header and footer” from the TCL log
  2. read the hostname of the switch and generate a file named by “switch hostname”
  3. write the hostname and the ip address of the switch in the header
  4. read vlans_per_interface file and remove speed and duplex, replace the vlan id with the vlan name in vlans_per_id file
  5. generate a third file named by  “switch hostname.map”
notes :
  1. i’m using awk to replace vlan_id with vlan_name
  2. i delete all the generated files and keep the needed ones
  3. i did the parser in bash because i know bash pretty good
  4. i used awk and sed in a different ways in order for the people who want to experiment on them
  5. the script could be written in one million way but i used this way for educational purposes, so please excuse me geeks of the world 🙂
#!/bin/bash -x
# Prepared by sghaida
#date : Wed Mar 21 21:11:48 EET 2012

FILE=vlans_per_id
VLANS=vlans_per_interface

sed -i -e '1,2d' $FILE
sed -i -e '2,4d' $FILE
sed -i -e '$d' $FILE

DICTIONARY=`cat $FILE |head -n 1|awk -F\# '{print $1}'`
sed '1d' $FILE > $DICTIONARY;rm -rf $FILE

echo "Hostname   : $DICTIONARY " > $DICTIONARY.MAP
echo "IP Address : $1 " >> $DICTIONARY.MAP
echo " " >>  $DICTIONARY.MAP
echo -e "port \t vlan \t status" >>  $DICTIONARY.MAP
echo "" >>  $DICTIONARY.MAP
echo -e "---- \t ---- \t ------ \t" >>  $DICTIONARY.MAP
echo "" >>  $DICTIONARY.MAP

sed -i -e '1,5d' $VLANS
sed -i -e '$d' $VLANS
sed -i -e '$d' $VLANS
sed -i -e '$d' $VLANS 

cat $VLANS |awk '{print $1 "\t""\t" $3 "\t""\t" $2}' > $VLANS.TMP
mv $VLANS.TMP $VLANS;

while read line
do
        vlan_id=`echo $line|awk '{print $2}'`

        if [ $vlan_id == "trunk" ];then
               vlan_name="trunk"
	       continue
        fi

	while read another_line
        do
                vlan_id2=`echo $another_line |awk '{print $1}'`
                vlan_name2=`echo $another_line |awk '{print $2}'`

                if [ $vlan_id -eq $vlan_id2 ];then
                        vlan_name=$vlan_name2
                        break
                else
                        continue
                fi
        done < $DICTIONARY
	echo -e "$line"|awk -v v1="$vlan_name" -v v2="$vlan_id"\
	'{ sub(v2,v1,$2); print $1" \t "$2" \t "$3 }' >> $DICTIONARY.MAP

done<$VLANS

rm -rf $VLANS
Posted in Network, Programming, Shell Scripting, System Administartion and tagged , , , , , , , , . Bookmark the permalink. RSS feed for this post. Leave a trackback.

5 Responses to Parsing Cisco Configuration using TCL and bash scripts

  1. Really nice article, arrived here from linkedin.
    I’ll use it for my future reference.
    Cheers
    Pedro

  2. ronak says:

    hi can u help me for creat vlan in cisco Switch per expect script .i Need it .thank u

  3. ronak says:

    hi .can u help me for creat vlans on cisco Switch per expect script .thank u

  4. Quintin says:

    Hi

    I am hoping you can help, am looking for someone who has great knowladge of bash . and scripts to change cisco configs.
    please lep me know
    thanx

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Swedish Greys - a WordPress theme from Nordic Themepark.