Guest

Cisco Catalyst 6500 Series Switches

Cisco IOS Shell CLI Module Configuration Example

Document ID: 116208

Updated: Jul 05, 2013

Contributed by Oleksandr Nesterov, Cisco TAC Engineer.

   Print

Introduction

This document describes the Cisco IOS® Shell (IOS.sh) CLI module.

Prerequisites

Requirements

Cisco recommends that you have knowledge of Cisco IOS Release 15.1(4)M, 15.1(2)S, and later releases. Cisco IOS.sh must be configured and enabled in order to use the Cisco IOS.sh features and functions on your router.

Components Used

The information in this document is based on these hardware and software versions:

  • Cisco Catalyst 6500 Series switches
  • Cisco IOS Release 15.1(1)SY

The information in this document was created from the devices in a specific lab environment. All of the devices used in this document started with a cleared (default) configuration. If your network is live, make sure that you understand the potential impact of any command.

Background Information

Cisco IOS.sh is a module that allows you to use shell scripting in the Cisco IOS CLI. This module helps automate routine actions and simplifies device management. It can be used in conjunction with other management tools such as Cisco IOS Embedded Event Manager (EEM).

Configure

Note: Use the Command Lookup Tool (registered customers only) in order to obtain more information on the commands used in this section.

In order to enable Shell CLI on the device, enter:

Switch#terminal shell

In order to disable the Shell CLI, enter:

Switch#terminal no shell

This feature supports the PATH variable, which contains a list of directories where shell can search for function/applets. Also, the Shell CLI supports these features:

Pipes and Redirection

Switch#show version | grep 15.1
Cisco IOS Software, s2t54 Software (s2t54-ADVENTERPRISEK9-M),
Version 15.1(1)SY, RELEASE SOFTWARE (fc2)

Switch#show version | grep 15 > bootdisk:version.txt
Switch#dir bootdisk:
Directory of bootdisk:/

14 -rw- 280 Nov 15 2012 23:25:32 +00:00 version.txt

1024557056 bytes total (577273856 bytes free)

Switch#cat bootdisk:version.txt
Cisco IOS Software, s2t54 Software (s2t54-ADVENTERPRISEK9-M),
Version 15.1(1)SY, RELEASE SOFTWARE (fc2)

Embedded Applets

Cisco IOS.sh contains several built-in applets that help to create applications and filters for the user functions:

[[             evaluate a logical test expression
cat            output data from a pipe or file to the terminal
cut            edit piped output
echo           echo arguments to the terminal
false          return false in while or if expressions, and set the result
fetch          return values from the configuration database
grep           search for regular expressions in piped output or files
head           print the first lines in the input
interface      print interfaces that match the argument
let            evaluate a numeric expression, and set the result
man            print information for built-ins
more           page piped output to the terminal
nl             number the lines in the input
null           ignore the input
printf         output formatted data to the terminal
read           read input into variables
set_oper       set operational values
sleep          pause execution of the terminal
sort           sort the input
tail           print the tail of the input
true           return true in while or if expressions, and set the result
uname          print system information
wc             count lines, words, and characters

Conditions

Here are the commands for the if....then....else operator.

Switch#x=5
Switch#if [[ $x != 5 ]];
then.else.fi>then
then.else.fi>echo false;
then.else.fi>else
else..fi>echo true;
else..fi>fi;
true
Switch#

These comparison operators are used in order to work with integer values:

OperatorsDescription
-eqArg1-eq Arg2. True if Arg1 is equal to Arg2
-neArg1-ne Arg2. True if Arg1 is not equal to Arg2
-ltArg1-lt Arg2. True if Arg1 is less than Arg2
-gtArg1-gt Arg2. True if  Arg1 is greater than Arg2
-geArg1-ge Arg2. True if Arg1 is greate than or equal to Arg2
-leArg1-le Arg2. True if Arg1 is less than or equal to Arg2

These conditions are available in order to work with files:

OperatorsDescription
-a or -eTrue if file exists
-dTrue if file exist and it is a directory
-fTrue if file exists and is a regular file
-rTrue if file exists and is readable
-sTrue if file exists and has a size greater than zero
-wTrue if file exists and is executable
-ntTest if file1 is newer than file2. The modification date on the file is used for this comparison
-otTest if file1 is older than  file2

In order to use several conditions within a comparison operator, you can use these logical operators:

  &&    Logical AND
  ||    Logical OR

Loops

Loops are the same as in Linux shell. Two operators are available.


Here are the commands for the for operator:

Switch#for x in 11 
do..done>do
do..done>ping 192.0.2.$x
do..done>done
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.0.2.10,timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
Switch#

Here are the commands for the while operator:

Switch#for i in 1 2 3
do..done>do
do..done>while [ $i -lt 3 ]
do..done>do
do..done>echo $i
do..done>done
do..done>i=i+1
do..done>done
Switch#

String Operations

For string comparisons, the if....then....else operator can be used with these modifications:

OperatorsDescription
==String 1 == string2, True if the strings are equal.
!=String1 != string2 , True if the strings are not equal
<String1 < string2, True if string1 is lexicographically  less than string2
>String1 > string2, True if string1 is lexicographically greater than string2

Configuration Example

Switch#function add_desc() {
{..} >show running-config interface Gi$1
{..} >configuration terminal
{..} >interface Gi$1
{..} >description $2
{..} >end
{..} >show running-config interface Gi$1
{..} >}
Switch#add_desc 5/1 SHELL_TEST
Building configuration...

Current configuration : 74 bytes
!
interface GigabitEthernet5/1
ip address 192.0.2.10 255.255.255.0
end

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

Current configuration : 98 bytes
!
interface GigabitEthernet5/1
description SHELL_TEST
ip address 192.0.2.10 255.255.255.0
end

Switch#

Verify

Use this section in order to confirm that your configuration works properly.

Enter the show terminal command in order to display the current terminal configuration:

Switch#show terminal 
Line 2, Location: "", Type: "XTERM-COLOR"
Length: 44 lines, Width: 155 columns
Baud rate (TX/RX) is 9600/9600
Status: PSI Enabled, Ready, Active, No Exit Banner, Ctrl-c Enabled
........
Preferred transport is lat.
Shell: enabled
Shell trace: off

Enter the show shell functions command in order to display all the functions available in the module, which include user-defined functions:

Switch#show shell functions
 #User defined functions:

Function namespace: DEFAULT
function add_desc()
{
 show running-config interface Gi$1
 configure terminal
 interface Gi$1
 description $2
 end
 show running-config interface Gi$1
}

Troubleshoot

There is currently no specific troubleshooting information available for this configuration.

Related Information

Updated: Jul 05, 2013
Document ID: 116208