This document describes the Cisco IOS® Shell (IOS.sh) CLI module.
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.
The information in this document is based on these hardware and software versions:
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.
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).
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:
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)
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
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:
Operators | Description |
-eq | Arg1-eq Arg2. True if Arg1 is equal to Arg2 |
-ne | Arg1-ne Arg2. True if Arg1 is not equal to Arg2 |
-lt | Arg1-lt Arg2. True if Arg1 is less than Arg2 |
-gt | Arg1-gt Arg2. True if Arg1 is greater than Arg2 |
-ge | Arg1-ge Arg2. True if Arg1 is greate than or equal to Arg2 |
-le | Arg1-le Arg2. True if Arg1 is less than or equal to Arg2 |
These conditions are available in order to work with files:
Operators | Description |
-a or -e | True if file exists |
-d | True if file exist and it is a directory |
-f | True if file exists and is a regular file |
-r | True if file exists and is readable |
-s | True if file exists and has a size greater than zero |
-w | True if file exists and is executable |
-nt | Test if file1 is newer than file2. The modification date on the file is used for this comparison |
-ot | Test 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 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#
For string comparisons, the if....then....else operator can be used with these modifications:
Operators | Description |
== | 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 |
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#
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
}
There is currently no specific troubleshooting information available for this configuration.
Revision | Publish Date | Comments |
---|---|---|
1.0 |
05-Jul-2013 |
Initial Release |