TCL Expect tutorial with Cisco examples

This tutorial explains how to combine tcl and expect to automate Cisco commands. This tutorial is a sample content from the ebook Network automation with TCL and expect

What is tcl

TCL stands for Tool command language. It is used for developing scripts for network testing activity. TCL works on Linux and Windows platforms. The procedure to install and setup TCL on Ubuntu is shown below.

TCL Setup procedure

The synaptic package manager available on Linux platforms (Ubuntu) can be used for setting up TCP. On the synaptic package manager search for TCL and install the package. The screenshot is shown below.

It can be observed that TCL version 8.5.0-2 is installed and setup on the Ubuntu system.

What is Expect

Expect is a framework, which is used for automating connecting with systems requiring remote access protocols like SSH, Telnet, FTP etc. In a network testing environment, it is widely used in network testing scenarios, where scripts for testing are executed from a remote system.

Expect Setup procedure

The synaptic package manager available on Linux platforms (Ubuntu) can be used for setting up Expect. On the synaptic package manager search for Expect and install the package. The screenshot is provided in the ebook Network automation with TCL and expect

It can be observed that expect package 5.45 -2build1 is installed on the system.

Understanding the need for TCL and Expect

A network testing environment requires, testing of features and protocols available on the product. Network test engineers typically develop TCL scripts which would be used for performing tests. These scripts have to be executed on the device under test.

Vendors like Cisco have the TCL language, installed as part of the operating system, which makes it possible to run the script directly on the router. Some vendors do not have the TCL language available as part of the operating system, in which case expect comes in handy.

In the topology (Topology included in the ebook Network automation with TCL and expect the network tester has developed scripts, which needs to be executed on the router. For this purpose, the tester first needs to establish connectivity with the router using commands like telnet, ssh, ftp etc. To automate the connectivity process expect is used along with TCL.

TCL and Expect Basics

Creating a TCL File

The vi editor can be used to create a TCL file from the command line on an Ubuntu system. To create a file with the name test.tcl, the command vi test.tcl can be used on the command line.

Import expect into a TCL file

After creating the file, the command package require Expect is to be typed in the file. The screenshot of file (Included in the Ebook Network automation with TCL and expect) with the command on an Ubuntu system.

Executing the TCL file.

To run the TCL file, the command sudo tclsh test.tcl can be used.

TCL essential for network automation

Variable initialization

To initialize a variable the set command is used. For ex, to assign the variable i with the value 3, set i 3 is used.

Incrementing values

To increment the value of the above variable i, the command incr i is used.

Printing values

The command puts is used to print value of variables, buffers on the screen.

While loop

A basic while loop command syntax is provided below.
set i 2
while {$i < 30} { puts "Hi" incr i} The loop will print the value Hi till the value of i is less than 30.

Expect commands

1. spawn

The spawn command is used for initiating the connection to the device under test with appropriate protocols like telnet , ssh, ftp etc. For ex, to initiate a telnet connection to the device with the IP address, 192.168.1.2, the command spawn telnet 192.168.1.2 is used.

2. expect

After the expect command is executed, the device responds with appropriate commands. For ex, after telnet is initiated to a router, the next prompt from the router would be “password:” . This is the string which is to be expected for which the command expect “password:” is used. The command expect is succeeded with the returned string, which in this case is “password:”

3. send

The send command is used to send the actual value which is expected by the device to which the connection has been initiated. For ex, in the above scenario, if the actual password value is “cisco”, this has to be sent the device, for which the command send “cisco” is used.

TCL and Expect Scripts

The source code of the scripts with detailed explanation are included in the ebook – Network automation with TCL and expect

Script to automate telnet connectivity to Cisco router.
Script to automate telnet connectivity to Cisco switch
Script to automate the process of printing the shown run command on a Cisco router
Script to search for a specific pattern in the output returned from a Cisco show command.
Script to automate the process of configuring RIP entry for a specific network address on a Cisco router.
Script to automate the process of configuring 100 RIP entries on a Cisco router
Script to automate the process of creating a VLAN on a Cisco switch.