Skip to content

Collect Read-Only Network Device Inventory with Python and Netmiko

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.


Step 1: Create an Isolated Python Environment

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
❯ View Expected Console Output
(.venv) $ python -c "import netmiko; print(netmiko.__version__)"

Step 2: Connect and Collect Only Read-Only Data

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}")
❯ View Expected Console Output
Device username: netops-readonly
Device password:
Saved report: inventory-192.0.2.10-20261004T101530Z.txt

Step 3: Review the Report and Handle Failures

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
❯ View Expected Console Output
Collected UTC: 20261004T101530Z
Device: edge-sw-01#
$ show version
...

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

Comments