Skip to content

Network Automation

My journey with Network & Cloud Automation

Menu
  • Beginner
  • DevOps-NetDevOps
  • Network Automation
    • Docker
    • Python Libraries
      • NAPALM
      • Netmiko
      • Jinja2
      • Scrapli
      • Yang
  • Cloud Automation
    • Terraform
  • Python 🐍 Tips and Tricks
Menu

How to read write csv with python for Network Automation Part 1

Posted on August 4, 2021August 5, 2021 by Gurpreet Kochar

We sure can build static dictionaries to interact with network devices for the purpose of network automation.

How to Automate Network Devices using Python Netmiko Part I

In the real world, this is not practical, we almost always will be required to read the device data from an inventory source which most of the time is going to be a csv file or an excel sheet. To demonstrate this, I am going to take Cisco Always ON Devnet Sandbox devices for example.

[FREE] Cisco Always on Devnet Sandbox Lab

I have converted the dictionary into a csv file that you can download for reference and the below screenshot has the directory structure that you need to set up for the python script.

cisco_always_on_devnet_sanbox_labDownload

Official Python CSV documentation can be found here

from netmiko import ConnectHandler
import csv

with open('cisco_always_on_devnet_sanbox_lab.csv', encoding='utf-8-sig') as f:
    for row in csv.reader(f):
        print(row)


╰─ python3 script2.py                                                                                                                                                                      ─╯
['HOST', 'SSH PORT', 'NETCONF PORT', 'RESTCONF PORT', 'USERNAME', 'PASSWORD', 'SECRET']
['sandbox-iosxe-latest-1.cisco.com', '22', '830', '443', 'developer', 'C1sco12345', 'C1sco12345']
['sandbox-iosxe-recomm-1.cisco.com', '22', '830', '443', 'developer', 'C1sco12345', 'C1sco12345']
['sandbox-iosxr-1.cisco.com', '22', '830', '', 'admin', 'C1sco12345', 'C1sco12345']
['sandbox-nxos-1.cisco.com', '22', '830', '443', 'admin', 'Admin_1234!', 'Admin_1234!']

Now that we can read the data, we can build the data structure required by netmiko to interact with devices dynamically. A sample data structure that netmiko requires is

csr1000v1 = {
    'device_type': 'cisco_xe',
    'host':   'sandbox-iosxe-latest-1.cisco.com',
    'username': 'developer',
    'password': 'C1sco12345',
    'port' : 22,                # optional, defaults to 22
    'secret': 'C1sco12345',     # optional, defaults to ''
}

We are using pprint ( pretty print ) module to print the formatted output of the dictionary for easy reading

from netmiko import ConnectHandler
import csv
from pprint import pprint


device_list = []
with open('cisco_always_on_devnet_sanbox_lab.csv', encoding='utf-8-sig') as f:
    for row in csv.reader(f):
        device_dict = {
            'host' : row[0],
            'device_type' : row[1],
            'username' : row[5],
            'password' : row[6],
            'secret' : row[7],
            }
        device_list.append(device_dict)

pprint(device_list)


─ python3 script2.py                                                                                                                                                                      ─╯
[{'device_type': 'device_type',
  'host': 'HOST',
  'password': 'PASSWORD',
  'secret': 'SECRET',
  'username': 'USERNAME'},
 {'device_type': 'cisco_xe',
  'host': 'sandbox-iosxe-latest-1.cisco.com',
  'password': 'C1sco12345',
  'secret': 'C1sco12345',
  'username': 'developer'},
 {'device_type': 'cisco_xe',
  'host': 'sandbox-iosxe-recomm-1.cisco.com',
  'password': 'C1sco12345',
  'secret': 'C1sco12345',
  'username': 'developer'},
 {'device_type': 'cisco_xr',
  'host': 'sandbox-iosxr-1.cisco.com',
  'password': 'C1sco12345',
  'secret': 'C1sco12345',
  'username': 'admin'},
 {'device_type': 'cisco_nxos',
  'host': 'sandbox-nxos-1.cisco.com',
  'password': 'Admin_1234!',
  'secret': 'Admin_1234!',
  'username': 'admin'}]

The problem here is, the first dictionary contains the header information of the csv sheet and there are multiple ways to get around this problem. We can either delete the first index of this list or we can use next() before we create dictionaries.

METHOD1:-

from netmiko import ConnectHandler
import csv
from pprint import pprint


device_list = []
with open('cisco_always_on_devnet_sanbox_lab.csv', encoding='utf-8-sig') as f:
    next(f)
    for row in csv.reader(f):
        device_dict = {
            'host' : row[0],
            'device_type' : row[1],
            'username' : row[5],
            'password' : row[6],
            'secret' : row[7],
            }
        device_list.append(device_dict)

pprint(device_list)



╰─ python3 script2.py                                                                                                                                                                      ─╯
[{'device_type': 'cisco_xe',
  'host': 'sandbox-iosxe-latest-1.cisco.com',
  'password': 'C1sco12345',
  'secret': 'C1sco12345',
  'username': 'developer'},
 {'device_type': 'cisco_xe',
  'host': 'sandbox-iosxe-recomm-1.cisco.com',
  'password': 'C1sco12345',
  'secret': 'C1sco12345',
  'username': 'developer'},
 {'device_type': 'cisco_xr',
  'host': 'sandbox-iosxr-1.cisco.com',
  'password': 'C1sco12345',
  'secret': 'C1sco12345',
  'username': 'admin'},
 {'device_type': 'cisco_nxos',
  'host': 'sandbox-nxos-1.cisco.com',
  'password': 'Admin_1234!',
  'secret': 'Admin_1234!',
  'username': 'admin'}]

METHOD2:-

Instead of using csvReader, we can leverage DictReader of csv module that will reader the header and the values in each row as a dictionary. Allows us to not manually having to handle the header line or use list index values but instead use header names to index.

from netmiko import ConnectHandler
import csv
from pprint import pprint


device_list = []
with open('cisco_always_on_devnet_sanbox_lab.csv', encoding='utf-8-sig') as f:
    for row in csv.DictReader(f):
        device_dict = {
            'host' : row['HOST'],
            'device_type' : row['device_type'],
            'username' : row['USERNAME'],
            'password' : row['PASSWORD'],
            'secret' : row['SECRET'],
        }
        device_list.append(device_dict)

pprint(device_list)

Read more about how to write csv files using python for network automation

Know someone who may benefit? Share this:

  • Tweet
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to email a link to a friend (Opens in new window) Email
  • More
  • Click to print (Opens in new window) Print
  • Click to share on Reddit (Opens in new window) Reddit
  • Share on Tumblr
  • Pocket

Like this:

Like Loading...

Related

2 thoughts on “How to read write csv with python for Network Automation Part 1”

  1. Pingback: How to read write csv with python for Network Automation Part 3
  2. Pingback: How to Install and Parse data with Netmiko TextFSM Plugin

Leave a ReplyCancel reply

All Blog Posts
My Resume

Upcoming Posts

Sorry - nothing planned yet!

Recent Posts

  • How to backup configuration to TFTP Server using Ansible – Part II
  • How to backup network devices using Ansible – Part I
  • Netmiko SSH Proxy/JumpServer
  • A short note on SASE
  • Understanding Ansible

Recent Comments

  1. Jack on Multithreading with Python for Network Engineers
  2. LifeCanvas on [Theory] Multithreading vs Multiprocessing vs AsyncIO
  3. Jasper Horng on Netmiko SSH Proxy/JumpServer
  4. asdfasdf on Python API Using FASTAPI – UPDATE – PUT – PATCH – Part V
  5. Gurpreet Kochar on Python Scrapli AsyncIO Usage

Archives

  • September 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
Topic Request / Suggestion
Loading
© 2025 Network Automation | Powered by Minimalist Blog WordPress Theme
 

Loading Comments...
 

    %d