• No results found

Introduction to Embedded Menu Manager

In document Tcl Scripting for Cisco IOS (Page 156-161)

Another feature in Cisco IOS that makes use of Tcl scripting is the Embedded Menu Manager (EMM). EMM was first introduced into Cisco IOS Software Release 12.4(20)T.

It enables you to create text-based menus written in XML that present Cisco IOS device users a simplified user interface. As part of the menu, you can use Tcl script language commands.

You can find the XML schema at the following site:

http://www.cisco.com/en/US/prod/iosswrel/ps6537/ps6555/ps9424/cisco_ios_

service_diagnostics_scripts.html

In XML, you need to tag every object with a start tag and end tag. The start tag is simply the element name, surrounded by < and > characters:

<name>

Closing the element requires a similar format, but with </ characters before the name and

> after the name:

</name>

The first required element in the schema is called menu. This must be present in the XML document, which defines the EMM menu.

You can begin by writing your own menu using the EMM XML schema document as a guide. The menu will be written in a Menu Definition File (MDF). First you must format the menu, as follows:

<Menu MenuName=”My First Menu” schemaVersion=”1.1”>

<MenuTitle>

</MenuTitle>

<Item>

<ItemTitle>

<Constant String=”This is the first item”/>

</ItemTitle>

</Item>

</Menu>

This is the simplest menu we could write. It is called My First Menu and has only one item called “This is the first item”. Save this in a text editor and call it my.mdf.

To test the menu, copy the file my.mdf to the Cisco IOS device, and then load the menu by telling EMM to try to read the MDF file, as follows:

Router#emm mdf flash:my.mdf 1. This is the first item Enter selection:

Chapter 5: Advanced Tcl Operation in Cisco IOS 141

Note MDF files can also be located on remote devices. MDF files can be loaded across the network on FTP, HTTP, NVRAM, RCP, and TFTP file systems.

The Cisco IOS device has read the menu definition file and immediately started the menu.

You can enter the only available choice, by typing number 1. After selecting 1, the menu immediately exits.

You can easily make a small change to the menu to show the current time as part of the menu display. To do this, you modify the first item to execute the Cisco IOS command show clock. There is an XML tag you can use called IOSExecCommand, which allows any Cisco IOS command to be run. One more XML tag needs to be added to Item, which is the ContinuePrompt. Without the ContinuePrompt, you would not see the results of the show command. In addition, you must add a second item to the menu, the ability to exit.

Edit the MDF script to contain the new item:

<Menu MenuName=”My First Menu” schemaVersion=”1.1”>

<MenuTitle>

</MenuTitle>

<Item ContinuePrompt=”true”>

<ItemTitle>

<Constant String=”This is the first item”/>

</ItemTitle>

<IOSExecCommand>”show clock”</IOSExecCommand>

</Item>

<Item>

<ItemTitle>

<Constant String=”Exit”/>

</ItemTitle>

</Item>

</Menu>

Copy the MDF script to the Cisco IOS device and use the following command to initiate the menu:

Router#emm mdf flash:my2.mdf 1. This is the first item 2. Exit

Press 1:

Enter selection:1

*19:04:01.003 PST Sun Jan 17 2010 Press any key to continue...

Select 2 to exit the script.

With basics covered, we will continue creating an EMM menu by adding a Tcl script command to the menu.

One Tcl script command previously used is string length, which tells you how many char-acters are in the given string. Another Tcl command you can use is hostname. If entered alone, it returns the hostname of the Cisco IOS device. You will use these two commands to count how many characters are in the hostname. To perform that function, you can enter the following command in a Tcl shell and view the length of the hostname string:

Router(tcl)#hostname Router

Router(tcl)#string length [hostname]

6

Router(tcl)#

To configure the menu to perform the same Tcl script function to count the hostname length, edit the MDF to contain the new items that will perform the counting.

You must add one new item, which uses the XML tags EmbeddedTCL and TCLCommand:

<Item ContinuePrompt=”true”>

<ItemTitle>

<Constant String=”Count Hostname Length”/>

</ItemTitle>

<EmbeddedTCL>

<TCLCommand>return [string length [hostname]]</TCLCommand>

</EmbeddedTCL>

</Item>

After you add the new item to the MDF file, the complete file contains the following:

<Menu MenuName=”My First Menu” schemaVersion=”1.1”>

<MenuTitle>

</MenuTitle>

<Item ContinuePrompt=”true”>

<ItemTitle>

<Constant String=”This is the first item”/>

</ItemTitle>

<IOSExecCommand>”show clock”</IOSExecCommand>

</Item>

<Item ContinuePrompt=”true”>

<ItemTitle>

<Constant String=”Count Hostname Length”/>

</ItemTitle>

<EmbeddedTCL>

Chapter 5: Advanced Tcl Operation in Cisco IOS 143

<TCLCommand>return [string length [hostname]]</TCLCommand>

</EmbeddedTCL>

</Item>

<Item>

<ItemTitle>

<Constant String=”Exit”/>

</ItemTitle>

</Item>

</Menu>

Copy the new MDF to the Cisco IOS device and monitor the results:

Router#emm mdf flash:my3.mdf 1. This is the first item 2. Count Hostname Length 3. Exit

Enter selection:2 9

Press any key to continue...

The menu was able to provide the correct number for the hostname string length.

One of the strengths of EMM is the capability to configure and preload the menus for partic-ular user accounts, by using the autocommand option. For example, configure the following:

Router(config)#emm mdf flash:my3.mdf Router(config)#user cisco password cisco

Router(config)#user cisco autocommand emm My First Menu Router(config)#end

Router#

*Mar 23 22:23:10.575: %SYS-5-CONFIG_I: Configured from console by console Router#conf t

Enter configuration commands, one per line. End with CNTL/Z.

Router(config)#line con 0 Router(config-line)#login local Router(config-line)#end

After configuring the preceding, try to log in and see the menu being displayed:

Username: cisco

Password: 1. This is the first item 2. Count Hostname Length

3. Exit

Enter selection:

Caution Use caution when enabling login local on the console. It is possible to lock yourself out from the router! Instead, consider enabling on login local on line vty 0 4 instead. That way, incoming Telnet sessions will be authenticated with a username and password.

We have demonstrated the ability to use Tcl script commands within the menu. The EMM feature is a powerful tool you can use to build simple user interfaces that allow nearly anything to be done on the Cisco IOS device. It is a great help in writing a cus-tomized application to be run on a Cisco IOS device that requires user input.

In document Tcl Scripting for Cisco IOS (Page 156-161)