Showing posts with label configurations. Show all posts
Showing posts with label configurations. Show all posts

Using TCL scripts and Cisco IOS Menus to switch between configurations

Following on from a previous post where we looked at managing multiple router configurations and how we could swap between them, I thought it would be nice to have this in a menu.

Something like this maybe?


Cisco IOS menu


Implementing menus on Cisco IOS is a fairly straight forward business, and there are plenty of examples on how to build menus that can do ping tests and trace routes. this one is slightly more complex as it involves the "config replace" command.

Before we go into how to create the menu it's important to point out an issue I had along the way. My original method, which was just calling the config replace command from the menu did not work as the router would hang on the following line:

*Sep  5 12:19:45.211: %PARSER-6-EXPOSEDLOCKRELEASED: Exclusive configuration lock released from terminal '0' -Process= "Exec", ipl= 0, pid= 3

And I would have to turn the router off and back on again, I could not find any way around this. So I had to find another method of doing this, and that was to incorporate a tcl script to get it to work.

Calling configure replace from tcl

Tcl (pronounced tickle) is a pretty cool scripting language, it's been around for a while and if you have done any Linux programming then it should be pretty familiar. We will definitely be revisiting tcl later on.

We start by entering the tcl shell by typing "tclsh" at the command prompt

Router#tclsh

Next we output the next commands to a file we are going to hold in flash (after we cd to flash and do a "mkdir myScripts" to create the directory). The w+ opens it in read/write mode.

Router(tcl)#puts [open flash:/myScripts/confrep2.tcl w+] {

the next lines are the commands that will be in the script.

+>tclsh
+>set labname [lindex $argv 0]
+>exec "configure replace flash:/myConfigs/$labname force"
+>}

So to explain, we need to specify the scripting language (tclsh), and then set a variable that will be passed so we name the variable "labname" and set it as the the first (0) argument ($argv). Then we give the actual command to execute, which is an exec mode argument "configure replace" and specify the path to the file, and again we specify that the argument passed in the calling of the script will be the filename (using $labname), lastly the "force" keyword is used to allow us to do the config replace without needing to wait or confirm anything.

The final line (the closing brace) matches up with the opening brace and tells the IOS that we have finished this script.

The screenshot below shows the complete syntax, along with exiting from the tclsh (remember to do this otherwise things will get weird...!).


tcl tclsh configure replace passing variable

Building a menu and submenu in Cisco IOS

For this menu we have two vendors to choose from (INE and Cisco 360) - and all this is is a factory-fresh config, with just the motd banner changed to show which lab, though closer to the end I did change the hostname on the different configs to make it more obvious.

This menu will allow us to select from two different vendors, and from there choose one of the two available labs via a sub-menu.

We start by creating the master menu:

menu ConfigMenu title ^ Config Menu ^
menu ConfigMenu prompt ^ Choose an option ^
menu ConfigMenu text 1 Load INE Configs
menu ConfigMenu command 1 menu INE
menu ConfigMenu text 2 Load 360 Configs
menu ConfigMenu command 2 menu 360
menu ConfigMenu text 3 Exit Menu
menu ConfigMenu command 3 exit
menu ConfigMenu line-mode

The first line gives the menu a title, and the second line sets a prompt for the user.
Line 3 and 5 set the text for the menu item and 4 and 6 then set the command that will be used, which in our case calls another menu (INE or 360). Finally on lines 7 and 8 we allow the user to exit out of the menu. the final two lines just make the menu look nicer.

The sub-menus

The INE and 360 menus are very similar to the first, but instead of calling a menu, we instruct the menu to call out tcl script.

menu INE title ^ Load INE Labs ^
menu INE prompt ^ Choose a lab to load: ^
menu INE text 1 Load INE Lab 1
menu INE command 1 tclsh flash:/myScripts/confrep2.tcl INE-1.txt
menu INE text 2 Load INE Lab 2
menu INE command 2 tclsh flash:/myScripts/confrep2.tcl INE-2.txt
menu INE text 3 exit to main menu
menu INE command 3 menu-exit
menu INE line-mode

The 360 is the same as the INE but with just the appropriate bits changed:

menu 360 title ^ Load 360 Labs ^
menu 360 prompt ^ Choose a lab to load: ^
menu 360 text 1 Load 360 Lab 1
menu 360 command 1 tclsh flash:/myScripts/confrep2.tcl 360-1.txt
menu 360 text 2 Load 360 Lab 2
menu 360 command 2 tclsh flash:/myScripts/confrep2.tcl 360-2.txt
menu 360 text 3 exit to main menu
menu 360 command 3 menu-exit
menu 360 line-mode

A sh run shows the completed menu


Cisco IOS menu with submenu

So now we can call the menu by issuing the command "menu ConfigMenu" from the prompt we can now see our new menu.

Cisco IOS menu in action

Cisco IOS menu

You need to press enter when you get the "Exclusive configuration lock" message, and also press 3 to exit the menu, but then you should get:


configure replace from menu tcl

So it is probably quicker than typing the full config replace, maybe only just, but then with the CCIE - timing is everything. It's also a good way to start working with tcl and there is a chance that you might be asked to configure a menu during the CCIE lab exam.

I hope to update this post with a start-to-finish video shortly.

Cisco IOS menu tips

1. Write it all out in notepad (or your text editor of choice) first, then you can paste it onto the router. This is especially important as if you try and remove a line from the menu it will actually delete the entire menu, so if you tye "no menu ConfigMenu command 3", you will lose all of ConfigMenu.

2. Make sure that the menu (once you have it working as you want it to) is on all your configs, otherwise you wont be able to use it to switch to another saved configuration.

Last note

I would like to give thanks to the guys over at blindhog for their post that really helped in getting this working!
Managing multiple configurations on Cisco IOS routers and switches

Managing multiple configurations on Cisco IOS routers and switches

In the journey to CCIE there are a great number of labs that we can be doing, such as INE, Narbik, CCIECert, Cisco 360 etc etc. So chances are our switches and routers are going to be reloaded a number of times with varying configurations.

Thankfully we can make the process a little easier and store a number of configurations on the equipment and change between these at our leisure.

The IOS supports a number of commands that can help us with this, so lets have a look at how we can manage the respective configs for a number of different labs.

The first command is mkdir, we can make a directory on the flash storage with the command
mkdir flash:/myConfigs
and confirm that its been created using the command
dir flash:
The output should look like this:
Router#dir flash
Directory of flash:/

1 -rw- 20570916 Jun 6 2000 19:19:48 +00:00 c1841-spservices9-mz.124.16b.bin
2 drw- 0 Jul 23 2013 11:11:42 +00:00 myConfigs

So now we have a nice place holder for our respective configs and we can begin filling this with our configuration files.

So lets make some very basic configurations. I am using an 1841 router, its got a serial interface that will connect to the BB1 router (the frame-relay backbone).

I assign a description to the Serial 0/0/0 interface:

Interface Serial0/0/0 
 description Link to BB1 - INE 

Like I said its a basic configuration! And now lets copy this to the myConfigs folder, and check that its there:
Router#copy run flash:/myConfigs/INE.txt
Destination filename [/myconfigs/INE.txt]?

694 bytes copied in 0.744 secs (933 bytes/sec)
Router#dir flash:/myconfigs
Directory of flash:/myConfigs/

12 -rw- 694 Jul 23 2013 11:20:28 +00:00 INE.txt

On the second line just hit enter to accept the offered destination filename.

So we now have one configuration. Lets make another one:

Router#conf t
Router(config)#int Serial 0/0/0
Router(config-if)#desc Link to BB1 - 360
Router(config-if)#exit
Router(config)#exit
Router#copy run flash:/myConfigs/360.txt
Destination filename [/myconfigs/360.txt]?

694 bytes copied in 1.268 secs (547 bytes/sec)
Router#dir flash:/myconfigs
Directory of flash:/myConfigs/

12 -rw- 694 Jul 23 2013 11:20:28 +00:00 INE.txt
13 -rw- 694 Jul 23 2013 11:24:16 +00:00 360.txt

So now we have two configurations - but how can we easily switch between them?

One option is to copy the file name back to the startup config and reload the router

Router#copy flash:/myConfigs/INE.txt startup-config 

But this means your router is down for a few minutes. Another way is to use the configure replace command.

At the moment our router is running the 360 config, we can see this by doing sh run | beg Serial0/0/0 and we can see the following:

Router#sh run | beg Serial0/0/0 
interface Serial0/0/0           
description Link to BB1 - 360   
no ip address                   
!                               

To make our router run the INE configuration we can use the command configure replace and specify the file we are going to substitute as the current running config:

Router#configure replace flash:/myConfigs/INE.txt    
This will apply all necessary additions and deletions    
to replace the current running configuration with the      
contents of the specified configuration file, which is      
assumed to be a complete configuration, not a partial       
configuration. Enter Y if you are sure you want to proceed. ? [no]: Y   
Total number of passes: 1        
Rollback Done                  

Router#                                                                  
*Jul 23 11:33:32.947: Rollback:Acquired Configuration lock.                
Router#                                                                 
*Jul 23 11:33:34.035: %PARSER-3-CONFIGNOTLOCKED: Unlock requested by process '3'. Configuration not locked.                                                        
Router#sh run | beg Serial0/0/0                                            
interface Serial0/0/0                                                        
description Link to BB1 - INE                                          
no ip address                                                              
!  

So now we can easily and quickly switch between multiple configurations!

We can also push multiple configurations onto the flash file system through tftp, or another method such as SCP. Here I am using SCP, firstly I copy a configuration to the SCP client hosted on a laptop, then I edited the file and saved it under the name CCIECert.txt, and then copied it back to the router. Lastly I did a configure replace to set it as the current running config:
Router#copy flash:/myConfigs/INE.txt scp:
Address or name of remote host []? 10.250.1.10
Destination username [Router]?
Destination filename [INE.txt]?
Writing INE.txt
Password:
!
694 bytes copied in 12.968 secs (54 bytes/sec)

Router#copy scp: flash:/myConfigs/CCIECert.txt
Address or name of remote host []? 10.250.1.10
Source username [Router]?
Source filename []? CCIECert.txt
Destination filename [/myConfigs/CCIECert.txt]?

Password:
!
699 bytes copied in 11.392 secs (61 bytes/sec)

Router#dir flash:/myConfigs
Directory of flash:/myConfigs/

   12  -rw-         694  Jul 23 2013 11:20:28 +00:00  INE.txt
   13  -rw-         694  Jul 23 2013 11:24:16 +00:00  360.txt
   14  -rw-         699  Jul 23 2013 12:25:12 +00:00  CCIECert.txt

Router#configure replace flash:/myConfigs/CCIECert.txt
This will apply all necessary additions and deletions
to replace the current running configuration with the
contents of the specified configuration file, which is
assumed to be a complete configuration, not a partial
configuration. Enter Y if you are sure you want to proceed. ? [no]: y
Total number of passes: 1
Rollback Done

Router#
*Jul 23 12:26:41.679: Rollback:Acquired Configuration lock.
*Jul 23 12:26:43.331: %PARSER-3-CONFIGNOTLOCKED: Unlock requested by process '3'. Configuration not locked.
Router#
Router#
*Jul 23 12:26:44.791: %LINK-5-CHANGED: Interface FastEthernet0/0, changed state to administratively down
*Jul 23 12:26:45.791: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to down
Router#sh run | beg Serial0/0/0
interface Serial0/0/0
 description Link to BB1 - CCIECert
 no ip address
!

So there we have it, its easy to host multiple configurations and switch between them quickly. just remember to save your work as you go!