Skip to content

Collect Read-Only Network Device Inventory with Python and Netmiko

Admin
October 4, 2026
8 min read
Intermediate
💻 Target OS: Cross-Platform (Python 3.10+ / Linux & Windows)

Before you begin

  • Applies to:Read-only network inventory collection with Python and Netmiko.
  • Where to start:Start from an approved management host that can reach the devices.
  • What you need:Have a list of authorized devices, a read-only account for the selected show commands, and a protected report destination.
Guide tools
Guide tools
0/0 Steps (0%)

Netmiko simplifies interactive SSH sessions to network devices from Python. This walkthrough connects to one Cisco IOS-style lab device, collects read-only command output, and saves it as a timestamped text report. You can extend the device list after validating the workflow on a device you administer.

Use a dedicated account with only the privileges needed to run the selected show commands. Do not store passwords in the script, source control, or a shared CSV file.


01

Install Netmiko in a Virtual Environment

Dependencies

Create a virtual environment so the project dependency does not change your system Python installation. Activate it before installing Netmiko, and use the same environment when running the script.

Terminal window
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install netmiko
❯ Illustrative Output (Results Vary)
(.venv) $ python -c "import netmiko; print(netmiko.__version__)"

02

Prompt for Credentials and Run Show Commands

Inventory

Save this as inventory.py. Netmiko’s device_type must match the platform driver for your device. Before connecting, enroll and verify the device’s SSH host key in the operator account’s known-hosts file. Strict checking below rejects unknown or changed keys. The password is requested interactively and is not echoed.

from getpass import getpass
from pathlib import Path
from datetime import datetime, timezone
from netmiko import ConnectHandler
device = {
"device_type": "cisco_ios",
"host": "192.0.2.10", # Replace with an authorized lab device.
"username": input("Device username: "),
"password": getpass("Device password: "),
"conn_timeout": 10,
"ssh_strict": True,
"system_host_keys": True,
}
commands = ["show version", "show ip interface brief"]
timestamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
report = Path(f"inventory-{device['host']}-{timestamp}.txt")
with ConnectHandler(**device) as connection:
prompt = connection.find_prompt()
with report.open("w", encoding="utf-8") as output:
output.write(f"Collected UTC: {timestamp}\nDevice: {prompt}\n")
for command in commands:
output.write(f"\n$ {command}\n")
output.write(connection.send_command(command, read_timeout=30))
output.write("\n")
print(f"Saved report: {report}")
❯ Illustrative Output (Results Vary)
Device username: netops-readonly
Device password:
Saved report: inventory-192.0.2.10-20261004T101530Z.txt

03

Check the Output Before Expanding the Inventory

Verification

Open the report and confirm the hostname, collection time, and command output are correct. If a connection fails, verify routing, SSH reachability, account permissions, and the correct Netmiko platform driver. Keep reports access-controlled because device details can expose network structure.

Terminal window
less inventory-192.0.2.10-20261004T101530Z.txt
❯ Illustrative Output (Results Vary)
Collected UTC: 20261004T101530Z
Device: edge-sw-01#
$ show version
...

See the Netmiko project documentation and its examples for supported platforms and connection options.

Comments