Cisco IOS.sh Shell

Cisco IOS shell (IOS.sh) provides shell scripting capability to the Cisco IOS command-line-interface (CLI).  Cisco IOS.sh enhances the process of controlling and configuring an IOS router using the CLI by including, variable substitution, paths, conditional statements, loops, pipes, functions, etc.

Lets cover some of the features and how we enable it, it’s disabled by default.

Prerequisites for Cisco IOS.sh
Cisco IOS Release 15.1(4)M, 15.1(2)S, and later releases.

Enabling Cisco IOS.sh
We have two methods two enable the IOS.sh feature.
1) terminal shell
2) shell processing full

When the terminal shell command is used, shell processing is not visible in the running configuration and is valid for that shell session, this provides quick and easy access to the shell commands.

Router>enable
Router# terminal shell
Router# terminal shell trace
Router# terminal no shell (disable shell processing)

To enable shell processing and access all its functions, it is recommended that you use the shell processing full command.

Router>enable
Router# conf t
Router(config)# shell processing full
Router(config)# no shell processing (disable shell processing)

After the Cisco IOS.sh is enabled, you can perform the following tasks:

  • Defining and using environment variables
  • Using control constructs to automate repetitive tasks
  • Creating and using Cisco IOS.sh functions
  • Using a new set of built-in function, that provide various text processing facilities.
  • Using extended pipelines to use the output of one command as input for another one.
  • Evaluating logical and arithmetic expressions for tests and variable setting.
  • Using online manual pages describing these changes.

Cisco IOS.sh Variables

To create a Variable use the following syntax (no spaces are allowed)
Router# VAR1=mystring
Router# VAR1= (clear/remove value)

To view the variables and values we can use the : show shell environment command.

You can use your defined variables from the Cisco CLI for example:
R1#VAR2=helloworld
R1#echo $VAR2
helloworld

PRC Variables

There are five built-in variables that are set on command completion. These variables indicate the status of the command that was just executed.

  • prc_change_mode
  • prc_change_type
  • prc_error_code
  • prc_failure_type
  • prc_ha_sync

The PRC variables contain the value of the PRC, as documented by IOS CLI documentation. In addition, the “$?” variable contains zero (0) on successful execution, and non-zero on failure. The use of the question mark is a historical precedent. Unfortunately, a question mark cannot be typed at the IOS prompt unless it is preceded by the IOS escape character ‘Ctrl-V’.

Cisco IOS.sh Control Constructs

Control constructs are used to conditionally or iteratively execute sets of commands.

  • if
  • for
  • while

“if” Statement
An “if” statement is used to conditionally execute sets of statements. The “if” loop syntax is as follows:

if <test>
statements
elif <test>
statements
else
statements
fi


The “elif” and “else” commands are optional, and there can be multiple “elif” statements. The “end” for “if” statements is “fi” which is “if” backwards. This is done for historical reasons.

There are a set of conditional tests available for tests, which are prefixed by either “[[” or “((“. (See conditional testing below).

In addition, built-in functions or CLI commands can be used as the test, and the PRC of the command will be used to determine TRUE or FALSE.

“for” Statement
A “for” loop is a control construct that loops over a given set of values, and executes a set of statements with a riable set to each of the values in turn. Here is an example:

for x in 1 2 3
do
ping 10.10.10.$x
done

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

“while” Statement
A ‘while” statement is used the same test syntax as an ‘if’ statement, and executes the statements in the following block until the test returns FALSE. Here is an example:

let x=0
while (( x++ < 10 )); do
echo $x
done


This will print out the numbers from 1 to 10

Loop Modifiers
Users can exit a ‘while’ or ‘for’ loop early by using a ‘break’ statement. Here is an example.

n=0
while true; do
let n++
if [[ $n -le 10 ]]; then
echo $n
else
break;
fi
done

In addition to ‘break’, the ‘continue’ statement can be used to stop executing statements in the loop, and restart the loop. A ‘while’ statement that is interrupted by a ‘continue’ will simply stop executing statements in the body of the loop and start the ‘while’ statement over again. A ‘for’ statement that is interrupted by a continue will also start over, except that it will assign the next element in the list of values before doing so.

Cisco IOS.sh Functions

IOS.sh functions enable you to define a group of commands that can be parameterized by command line arguments. The arguments passed to the IOS.sh function will be substituted for $1, $2, and so on within the body of the function. Here is an example of a user defined function:

Defining your own user functions
# helloworld example
function helloworld(){
echo helloworld
ping $1
}

Output:
R1#helloworld 10.10.10.1
helloworld
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.10.10.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/4 ms

To view the user defined and built-in functions we can use the following command:
show shell functions

Built-In Functions

IOS.sh has a number of complementary built-in functions that an be found in the table below:

To get more information on each of the commands use the : man option followed by the function name.

Built-in FunctionsDescription
catOutput data from a pipe or file to the terminal
cutEdit piped output
echoEcho arguments to the terminal
falseReturn false in while or if expressions, and set the result
fetchReturn values from the configuration database
forCisco IOS.sh for loops
grepSearch for regular expressions in piped output or files
headPrint the first lines in the input
interfacePrint interfaces that match the argument
letEvaluate a numeric expression, and set the result
manPrint information for built-ins
morePage piped output to the terminal
nlNumber the lines in the input
nullIgnore the input
printfOutput formatted data to the terminal
readRead input into variables
set_operSet operational values
sleepPause execution of the terminal
sortSort the input
tailPrint the tail of the input
trueReturn true in while or if expressions, and set the result
unamePrint system information
wcCount lines, words, and chars

Pipes and Redirections

The IOS CLI has a facility to ‘pipe’ text from a command into a set of programs that can filter or redirect the output. The Cisco IOS.sh has expanded this facility to support more than one ‘pipe’ command on a line of input. These can be used with the built-in IOS.sh functions.

An example could look like this : show int | grep GigabitEthernet | wc -l

External Links:

https://www.cisco.com/c/en/us/td/docs/ios/netmgmt/configuration/guide/Convert/IOS_Shell/nm_ios_shell.html

Leave a Comment

*