Sunday, December 1, 2013
Thursday, September 17, 2009
Thursday, July 9, 2009
Advanced Command scripts, with NCM, can be written in Perl or Expect. When there is interactive input, Expect is normally recommended. This blog provides a starting point for using Expect to aid in the creation of Cisco NCM command scripts
One of the best references for Expect is the “Exploring Expect” book published by O’Reilly. I’ll be adding page number references from the book to the sections below.
Another good reference is the included help files with ActiveState Expect.
One way to learn how to use Expect is to use the free version, from ActiveState, on your Windows PC. This provides the opportunity to learn how TCL and Expect work and test scripts before deploying within NCM. Use the following steps to download and install Expect
1. Go to http://www.activestate.com and download the free TCL application
2. Install TCL
3. Open a command prompt
4. Type “teacup install Expect” to install the Expect extension to TCL
Now that TCL and Expect are installed, you can run through the following test script to see how Expect works. This test script shows how to telnet to a router and execute “show version”. Within the example script, replace anything in red with the appropriate information from your test router.
There are minor differences between this script and the script executed within NCM. The one main difference is the use of the “exp_send” command. Within NCM, this should just be “send”.
1. Open Notepad
2. Paste the following script into Notepad and save as sample.txt. This script assumes that you have AAA turned on an authorization set to have the user authorized into privileged exec mode. If this configuration is not already entered, you can add it by entering the following to add AAA with local authentication and authorization
3. Run the script by opening a command prompt, navigating to the directory with the script, and executing “tclsh85 sample.txt”. The output of the script should look similar to the following
Expect scripts normally include the use of regular expressions to evaluate output from routers and switches. The best way to show how this works is through a simple example. Let’s take the output from “show interface fa1/0”. Here’s the output to use as a reference for the example to follow.
With this script, we want to evaluate the up/down state of the interface and line protocol and exit out of the script with an error message if either the interface or line are down.
Let’s look at the regular expression portion of the script
The “-re” specifies that the next portion should be treated as a regular expression (pg 109 Exploring Expect). The portion [a-z] specifies a single character matching any lower case character. The “*” after the [a-z] specifies a match for the rest of the lower case characters that follow the initial character. You’ll notice that there is a \ before the first [. This is required, by Expect, to have the [ ] treated correctly as a regular expression delineator (pg 91 Exploring Expect). The () specifiy that anything within them should be treated as a variable. The variable is saved in the line after the regular expression with the line (pg 111 Exploring Expect).
The “1” specifies that it is the first variable created by the (). The second () is captured as the variable “line_status”. You’ll notice that the “1” has been replaced by a “2” in this case (pg 111 Exploring Expect).
The next thing to notice is the “exp_continue” command. This specifies that the expect command that surrounds it should be run until there is no remaining input to be evaluated (pg 145 Exploring Expect).
Here is the output of the script
In the first script, we saw that there was a lot of output. We could limit this output to specific information that we wanted to display. This is done by changing “log_user 1” to “log_user 0”(pg 175 Exploring Expect). Once this is done, specific output can be displayed using the “puts” command. Here is the script from the regular expression section that will only output the interface information
Here is the output that is displayed in this case