2.2 Lab - CLI Automation With Python Using Netmiko

You are on page 1of 4
At a glance
Powered by AI
The key takeaways are that netmiko is a Python module that can be used to simplify automating tasks on network devices via SSH. It allows Python scripts to connect to devices, send configuration commands, and retrieve output.

Netmiko is a Python module for automating network tasks using SSH. It provides functions to simplify establishing SSH connections and sending commands to network devices. This allows administrators to remotely execute commands and retrieve outputs programmatically.

Netmiko uses the ConnectHandler function to establish an SSH connection to a network device. After connecting, the send_command and send_config_set functions can be used to execute commands in exec mode or send configuration commands respectively and retrieve the output.

Lab – CLI Automation with Python using netmiko

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

Part 1: Install the netmiko Python module


In this part, you will install netmiko module into your Python environment. Netmiko is a python module that
simplifies ssh CLI connection to network devices. It has built in functionality to identify to execute “exec mode”
commands, as well as apply new commands in the running configuration.
Explore the netmiko module on the project GitHub repository: https://github.com/ktbyers/netmiko

Step 1: Use pip to install netmiko.


a. Start a new Windows command prompt (cmd).
b. Install netmiko using pip in the Windows command prompt:
pip install netmiko
c. Verify that netmiko has been successfully installed. Start Python IDLE and in the interactive shell try to
import the netmiko module:
import netmiko

© 2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 4 www.netacad.com
Lab – CLI Automation with Python using netmiko

Part 2: Connect to IOS XE’s SSH service using netmiko

Connect to IOS XE’s SSH service using netmiko.


The netmiko module provides a “ConnectHandler()” function to setup the remote ssh connection. After a
successful connection, the returned object represents the ssh cli connection to the remote device.
a. In Python IDLE, create a new Python script file:

b. In the new Python script file editor, import the “ConnectHandler()” function from the netmiko module:

from netmiko import ConnectHandler

c. Setup a sshCli connection object using the ConnectHandler() function to the IOS XE device.

The parameters of the ConnectHandler() function are:


• device_type – identifies the remote device type
• host – the address (host or IP) of the remote device (adjust the IP address “192.168.56.101”
to match your router’s current address)
• port – the remote port of the ssh service
• username – remote ssh username (in this lab “cisco” for that was setup in the IOS XE VM)
• password – remote ssh password (in this lab “cisco123!” for that was setup in the IOS XE
VM)

Part 3: Use netmiko to gather information from the device

Send show commands and display the output


a. Using the sshCli object, returned by the ConnectHandler() function that represents the ssh cli
remote session, send some “show” command and print the output. Use the send_command() function of
the sshCli object with a string parameter that represents the command you wish to execute in the exec
mode:

© 2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 4 www.netacad.com
Lab – CLI Automation with Python using netmiko

b. Execute the Python script file to see the results.

If you have not saved the script file yet, you will be prompted to save it before it is executed.
c. Verify the results:

Quesion

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?

Type your answers here

Part 4: Use netmiko to alter configuration on the device


In the following steps, you will alter the configuration of the device by creating new loopback interfaces.

Create a new loopback interface


Using the sshCli object, returned by the ConnectHandler() function that represents the ssh cli remote
session, send some configuration command and print the output. Use the send_config_set() function of the
sshCli object with a list parameter including the configuration commands as strings you wish to execute in the
exec mode:
config_commands = [
'int loopback 1',
'ip address 2.2.2.2 255.255.255.0',
'description WHATEVER'
]

output = sshCli.send_config_set(config_commands)

Execute the Python script file and verify the results


Questions
Questions

Why does the output from “show ip int brief” not include the “loopback1” interface?

Type your answers here

How to execute and display the output from the “show ip int brief” command after the loopback interfaces was
created?

Type your answers here


End of questions
End of Questions

© 2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 4 www.netacad.com
Lab – CLI Automation with Python using netmiko

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.
Questions

Was the new loopback2 interface successfully created?

Type your answer here

Was the new configuration change accepted, partially accepted or rejected?


Type your answer here

End of Document

© 2017 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 4 www.netacad.com

You might also like