Cisco IOS Python Script

This guide explains how to use a Python script to run EXEC commands and also make configuration changes on multiple devices. This script uses a simple CSV list of switches and another CSV list of commands to send desired commands to many Cisco IOS devices at once. The script also creates a log after each sequence.

Introduction

Using a Python script to push commands to multiple Cisco IOS devices can offer several benefits, including automation, consistency, scalability, flexibility, and reusability. By automating the process of configuring multiple network devices, Python scripts can reduce the risk of human error, save time, and ensure consistency across devices. Additionally, Python scripts can be easily modified and scaled to meet the needs of any network configuration task.

Description
Automation: Manually configuring multiple network devices can be a time-consuming and error-prone process. A Python script can automate the process, reducing the risk of human error and saving time.
Consistency: When configuring multiple devices, it is important to ensure that the configuration is consistent across all devices. A Python script can push the same commands to all devices, ensuring consistency and reducing the risk of configuration errors.
Scalability: As the number of network devices increases, the time and effort required to manually configure them also increase. A Python script can easily scale to configure hundreds or even thousands of devices.
Flexibility: A Python script can be easily modified to push different sets of commands to different devices or groups of devices, allowing for greater flexibility in network configuration.
Reusability: Once a Python script has been developed and tested, it can be reused to configure other similar devices, reducing the time and effort required for future network configuration tasks.

The topology below will be used to demonstrate the script.

Script Folder

The script folder can be downloaded at the bottom of this guide. The folder contains two sub-folders. The EXEC command folder contains a python script and the CSV files to push EXEC commands. This means, all types of show commands, copy commands, ping and the write command for example. The configuration folder contains another script and CSV lists to push configuration changes to Cisco IOS devices. Each script sequence generates a log file for the user to review the CLI output.

EXEC Command Folder
List of switches
List of EXEC commands
Script
Output log (after every script sequence)
Configuration Folder
List of switches
List of configuration commands
Script
Output log (after every script sequence)

The following folder is used to push EXEC commands to multiple Cisco IOS devices.

The “Configure Terminal” folder is used to push configuration changes to multiple Cisco IOS devices.

Install Python

The following steps show how to install Python and the Netmiko SSH library.

Description
1. Visit https://python.org and download the current python version available for your operating system
2. Install python on your system
3. Open a terminal or command line interface (CMD) on your operating system
4. Use the command "pip install netmiko" to install the SSH python library
5. Restart your PC system
CMD Command Prompt

Microsoft Windows [Version 10.0.22621.1413]
(c) Microsoft Corporation. All rights reserved.

C:\Users\NBLAB01>pip install netmiko
pip install netmiko

Microsoft Windows [Version 10.0.22621.1413]
(c) Microsoft Corporation. All rights reserved.

C:\Users\NBLAB01>pip install netmiko
Collecting netmiko
  Downloading netmiko-4.1.2-py3-none-any.whl (196 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 196.8/196.8 kB 2.0 MB/s eta 0:00:00
Collecting ntc-templates>=2.0.0
  Downloading ntc_templates-3.3.0-py3-none-any.whl (372 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 372.6/372.6 kB 2.6 MB/s eta 0:00:00
Collecting scp>=0.13.3
  Downloading scp-0.14.5-py2.py3-none-any.whl (8.7 kB)
Collecting textfsm==1.1.2
  Downloading textfsm-1.1.2-py2.py3-none-any.whl (44 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.7/44.7 kB 2.3 MB/s eta 0:00:00
Collecting pyyaml>=5.3
  Downloading PyYAML-6.0-cp310-cp310-win_amd64.whl (151 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 151.7/151.7 kB 2.3 MB/s eta 0:00:00
Collecting paramiko>=2.7.2
  Downloading paramiko-3.1.0-py3-none-any.whl (211 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.2/211.2 kB 1.2 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=38.4.0 in c:\users\nblab01\appdata\local\programs\python\python310\lib\site-packages (from netmiko) (65.5.0)
Collecting tenacity
  Downloading tenacity-8.2.2-py3-none-any.whl (24 kB)
Collecting pyserial
  Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.6/90.6 kB 2.6 MB/s eta 0:00:00
Collecting future
  Downloading future-0.18.3.tar.gz (840 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 840.9/840.9 kB 1.7 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting six
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting cryptography>=3.3
  Downloading cryptography-40.0.1-cp36-abi3-win_amd64.whl (2.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.6/2.6 MB 3.3 MB/s eta 0:00:00
Collecting bcrypt>=3.2
  Downloading bcrypt-4.0.1-cp36-abi3-win_amd64.whl (152 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 152.9/152.9 kB 1.1 MB/s eta 0:00:00
Collecting pynacl>=1.5
  Downloading PyNaCl-1.5.0-cp36-abi3-win_amd64.whl (212 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.1/212.1 kB 4.3 MB/s eta 0:00:00
Collecting cffi>=1.12
  Downloading cffi-1.15.1-cp310-cp310-win_amd64.whl (179 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 179.1/179.1 kB 470.2 kB/s eta 0:00:00
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 118.7/118.7 kB 1.4 MB/s eta 0:00:00
Installing collected packages: pyserial, tenacity, six, pyyaml, pycparser, future, bcrypt, textfsm, cffi, pynacl, ntc-templates, cryptography, paramiko, scp, netmiko
  DEPRECATION: future is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for future ... done
Successfully installed bcrypt-4.0.1 cffi-1.15.1 cryptography-40.0.1 future-0.18.3 netmiko-4.1.2 ntc-templates-3.3.0 paramiko-3.1.0 pycparser-2.21 pynacl-1.5.0 pyserial-3.5 pyyaml-6.0 scp-0.14.5 six-1.16.0 tenacity-8.2.2 textfsm-1.1.2

C:\Users\NBLAB01>

Premium Access

Log in or register - Premium members can access all content without restrictions.

Get Premium

0 Comments

Submit a Comment