2.2 Lab - CLI Automation With Python Using Netmiko - ILM
2.2 Lab - CLI Automation With Python Using Netmiko - ILM
Version)
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
Objectives
Part 1: Install the netmiko Python module
Part 2: Connect to IOS XE’s SSH service using netmiko
Part 3: Use netmiko to gather information from the device
Part 4: Use netmiko to alter configuration on the device
Background / Scenario
For simple network automation using a remote telnet or ssh based command line, network administrators
have been using various screen scraping techniques for a long period of time. Initially the “expect” based
scripts we utilized to automate entering commands when a specific expected string appeared on the
command line. With the evolution of the Python language, the netmiko Python module has emerged as an
open source project hosted and maintained on GitHub.com that provides a simple network automation
interface using similar techniques like the “expect” based scripts.
In this lab activity, you will identify the potential but also the limitations of using netmiko to transport CLI
commands for network automation.
Required Resources
Access to a router with the IOS XE operating system version 16.6 or higher.
Access to the Internet
Python 3.x environment
Instructions
2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 5 www.netacad.com
Lab – CLI Automation with Python using netmiko
b. In the new Python script file editor, import the “ConnectHandler()” function from the netmiko module:
c. Setup a sshCli connection object using the ConnectHandler() function to the IOS XE device.
2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 5 www.netacad.com
Lab – CLI Automation with Python using netmiko
If you have not saved the script file yet, you will be prompted to save it before it is executed.
c. Verify the results:
d. Verify the data type of the “output” variable. How would you extract the IP address and the Interface
Name into variables? What if there were multiple interfaces?
The “output” variable is of “string” type. Extracting a specific field from a string can be a non-trivial
operation. You could use Regular Expressions (in python the “re” module (i.e. “import re”)) to specify the
parts to be extracted. However, different versions of IOS might have slightly different output and therefore
you might need to build more advanced Regular Expressions to correctly handle different outputs.
output = sshCli.send_config_set(config_commands)
2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 5 www.netacad.com
Lab – CLI Automation with Python using netmiko
Questions
Why does the output from “show ip int brief” not include the “loopback1” interface?
At the point in time when the “show ip int brief” was executed, the loopback1 interface was not created yet. In was
only created once the “config_commands” list of configuration commands was send to the router using the
“send_config_set” command.
How to execute and display the output from the “show ip int brief” command after the loopback interfaces was
created?
Run again the “send_command” function with the “show ip int brief” command and print the returned output.
End of Questions
Add code to create a new loopback interface (loopback2) with the same IP address as on the existing loopback
interface, only with a different description.
Execute the Python script file and verify the results:
2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 5 www.netacad.com
Lab – CLI Automation with Python using netmiko
Questions
End of Document
2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 5 www.netacad.com