RESTCONF 103: Porting Your Network Scripts to RESTCONF

In this Learning Lab you will learn the basics of how to use RESTCONF to call Network APIs in Python along with the advantages it offers over scripts written in languages such as Task Control Language (TCL).

Objective

Completion Time: 25 minutes

  • Understand the use and application of a TCL script
  • Run and understand a RESTCONF script that does the same operation
  • Understand the advantages that RESTCONF offers over TCL

Prerequisites

Background

Access to a Cisco Cloud Services 1k Router (CSR1K)

  • To run these code samples, you will need access to a Cisco Cloud Services 1k Router with IOS-XE version 3.17 or later installed.
  • If you are not using your own CSR1K, you can use the DevNet Sandbox Always-On CSR1K Router which we'll refer to in this lab.

Python

  • To run the code samples, you need to have Python 3 installed on your machine.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, Python 3.4.2 is already installed.
  • See How to Set up Your Own Computer section above for how to install Python on your own machine.

Python Requests Library

  • These code samples use the Python Requests Library to simplify making REST API calls.
  • If you are working on a DevNet Learning Lab PC at a DevNet event, the Requests Library is already installed.
  • See How to Set up Your Own Computer section above for how to install the Requests Library on your own machine.

Clone Git Repo

  • If you are working on a DevNet Learning Lab PC at a DevNet event
    • Open the Git Command window by clicking on the Git CMD icon on the Task Bar or click on the Start button, then in the Run bar type: git cmd and press the Enter key.
  • If you are working from your own workstation, on your desktop open a command terminal.
  • Go to the root directory by typing: cd \
  • Create a directory called 'C:\DevNetCode\yourname' by typing: mkdir DevNetCode\<your-name>
    • For example: mkdir DevNetCode\brTiller
  • Go to the new directory by typing: cd \DevNetCode\<your-name>
    • For example: cd \DevNetCode\brTiller
  • Clone the coding skills sample code from GitHub. Enter the command below.
    git clone https://github.com/CiscoDevNet/restconf-examples
    


    Inside the directory you created you should now see directory 'restconf-examples'.



Step 1. Understanding the Task Control Language (TCL) Script

Let's assume that you need to write a script that gets basic information about network interfaces along with their status. This basic network information should be displayed on screen. The status of each interface should be reviewed, and finally, the overall status of all interfaces whether up or down should be displayed.

Below we've created a simple TCL script that a network engineer might write to complete this task. TCL is a very common language utilized by network engineers. The TCL script below calls a router Command Line Interface (CLI) command, 'show ip int brief' to get the interface data which is then parsed and processed for review.

#regular expression to call the CLI command 'show ip interface brief', capture and
#parse the returned data for interfaces with assign IP addresses.
set interfaces [regexp -all -line -inline "^(FastEthernet|GigabitEthernet)(\[0-9.\]+) +(\[0-9.\]+).*(up|down)" [exec "show ip int brief"]]

#variable to walk through the interfaces array of returned data.
set index 0
#variable to flag if an interface in 'down' status has been found.
set flagDown 0
#Loop through the interfaces array to the end.
while {$index < [llength $interfaces]} {
  #Assign the interface name and number to variable named interface
  set interface "[lindex $interfaces [expr $index + 1]] [lindex $interfaces [expr $index + 2]]"
  #Assign the interface IP and number to variable named ip
  set ip "[lindex $interfaces [expr $index + 3]]"
  #Assign the interface status and number to variable named status
  set status "[lindex $interfaces [expr $index + 4]]"
  #if an interface status is down set the flag
  if {$status == "down"} {
    set flagDown 1
 }
  #increment the counter to the next interface data
  set index [expr $index + 5]
  #write captured interface data to the screen.
  puts "interface:  $interface   ip:  $ip    status: $status"
}
#Report final status of all reviewed interfaces
if {$flagDown == 0} {
  puts "All interfaces are up"
} else {
  puts "At least one interface is down"
}

Let's look at what the code is doing.

  • First, a regular expression is written to capture the data output by the command 'show ip int brief'. This information is gathered from the screen data that the developer must analyze when the command is run.
    • The returned data is parsed via regular expression and captured by encasing the data to be evaluated in parenthesis which is then added to the interfaces variable.
  • Next, we iterate through the interfaces variables and assign parts of its data to other variables
    • The interface name, IP and status information are assign to various variables and displayed.
    • The status information is evaluated and the flagDown variable is set to 1 if an interface status of 'down' is found.
  • Finally, the status of the flagDown variable is checked and the corresponding message of the overall status of all interfaces is displayed.

Resulting output

For security reasons external users are not able to run the tcl script directly on the Cisco Always-On CSR1K in the DevNet sandbox. However, for learning purposes we've run the script on the router and can evaluate the output which is below. To review the TCL script on your workstation, access the '/DevNetCode//restconf-examples/restconf-103 directory, and open the file interfaceStatus.tcl .


Log into the router and access the TCL shell


interfaceStatus.tcl output is circled

Finally let's check the actual interfaces on the CSR1K router. Notice that the TCL script had captured only interfaces with assigned IPs and that were named GigabitEthernet. This filtered capture is due to the regular expression that was written to match the pattern of the data output to the screen. The network engineer must be careful to not accidentally exclude data that should be reviewed.
*Actual data output by CLI command

Things to Think About

  1. What are the problems with parsing data written to a screen?
  2. Are there any potential issues with running scripts directly on the router?

Step 2: Next we'll learn how to complete this operation using RESTCONF