Network Automation with Python and Netmiko

My Python scripts connect to a Cisco router with Netmiko, read the device information, tidy it with TextFSM and save a JSON report.

Overview

I wrote Python scripts that log in to a Cisco IOS router (R1) with Netmiko, run show version and show ip interface brief, turn the interface table into structured data with TextFSM and save it as a JSON report. My later version reads the device details from a JSON inventory file. The repository lists more stages I plan to add, such as Ansible and NETCONF. So far only the Python stage is published.

What I set out to do

  • Collect device information automatically instead of typing commands by hand.
  • Read the devices from an inventory file instead of hard-coding them.

What I did

  • device_info.py connects to R1, runs the show commands and picks out the hostname, IOS version, uptime and interfaces.
  • inventory_reader.py and inventory_connection.py read inventory/devices.json and open a Netmiko session.
  • collect_device_info.py puts both together and writes api/<hostname>-device-info.json.

Screenshots

They are in the same order as in my repository. Click one to see it full size.

How I tested it

  • My screenshots show the Netmiko connection to R1 and the structured output.
  • The report api/r1-device-info.json shows IOS 17.15.1 and four interfaces with their status.

Repository

My full notes and the original screenshots are on GitHub: network-infrastructure-and-automation