Cisco Shell

Cisco IOS shell ( provides shell scripting capability to the Cisco IOS command-line-interface (CLI).  Cisco 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
Cisco IOS Release 15.1(4)M, 15.1(2)S, and later releases.

Enabling Cisco
We have two methods two enable the 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# 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# conf t
Router(config)# shell processing full
Router(config)# no shell processing (disable shell processing)

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

  • Defining and using environment variables
  • Using control constructs to automate repetitive tasks
  • Creating and using Cisco 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 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#echo $VAR2

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 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>
elif <test>

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
ping 10.10.10.$x

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, 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, 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, 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

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.

while true; do
let n++
if [[ $n -le 10 ]]; then
echo $n

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 Functions functions enable you to define a group of commands that can be parameterized by command line arguments. The arguments passed to the 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

Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to, 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 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 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 has expanded this facility to support more than one ‘pipe’ command on a line of input. These can be used with the built-in functions.

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

External Links:

1 thought on “Cisco Shell”

Leave a Comment