Collect Read-Only Network Device Inventory with Python and Netmiko
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
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.
Install Netmiko in a Virtual Environment
DependenciesCreate 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.
python3 -m venv .venvsource .venv/bin/activatepython -m pip install --upgrade pippython -m pip install netmiko❯ Illustrative Output (Results Vary)
(.venv) $ python -c "import netmiko; print(netmiko.__version__)"Prompt for Credentials and Run Show Commands
InventorySave 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 getpassfrom pathlib import Pathfrom 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-readonlyDevice password:Saved report: inventory-192.0.2.10-20261004T101530Z.txtCheck the Output Before Expanding the Inventory
VerificationOpen 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.
less inventory-192.0.2.10-20261004T101530Z.txt❯ Illustrative Output (Results Vary)
Collected UTC: 20261004T101530ZDevice: edge-sw-01#
$ show version...See the Netmiko project documentation and its examples for supported platforms and connection options.
Your feedback keeps these production procedures accurate and battle-tested.