Advanced Python for Network Engineers · Python Adv

Multivendor SSH with netmiko: one script, every platform

Program 1 taught netmiko on Cisco with the lab name nk_ios. Real networks mix vendors. Learn the real device_type names for IOS XE, NX-OS, EOS, Junos and the firewalls, how each platform handles paging, config mode, commit and save, how to read structured output with ntc-templates on four vendors, how threads, retries, session logs and a diff against yesterday turn a script into a collector, and how to troubleshoot the errors that only show up away from Cisco.

49 min read10 chapters2 labs15 quiz7 scenarios15 interview Q&A

This first module is free: read the lesson and take the quiz. Labs need an account: sign up free to run up to 3 hands-on labs.

Log inStart free trial
This reading material cannot be printed. Read it on config.nwkings.com.
01

The big picture: one script, every platform

What you will learn. In Program 1 you used netmiko against Cisco routers with the lab name nk_ios. A real network is rarely all Cisco. This module teaches you to write one collector that logs in to an IOS XE router, a Junos router, an Arista EOS switch and an NX-OS switch (and, in the reference sections, ASA, FortiOS and PAN-OS firewalls) with the real netmiko names, and to run it safely every night.

Analogy: a travel adapter and a good interpreter

Imagine you travel to five countries with one laptop. The laptop is your script. The wall sockets are the vendors: each has a different shape, voltage and rule (some switch off the pager, some need a commit, some ask for a second password). netmiko is the travel adapter kit plus an interpreter: you tell it the device_type ("this wall is Junos") and it knows the shape of the plug, how to say "stop paging", how to enter config mode and how to leave it. Your job is to pick the right adapter, to know what each wall expects, and to notice when the lights go out.

inventory.csvname, host, device_type .envpasswords, not in code collector on AUTO01ConnectHandler(**dev)threads, retries, logs R1 IOS XE · cisco_ios J1 Junos · juniper_junos E1 EOS · arista_eos N1 NX-OS · cisco_nxos one loop, four dialects

The collector reads who to talk to from a file, takes passwords from the environment and speaks each vendor's dialect through its device_type.

The five questions every vendor answers differently

Almost every surprise in this module is one of five questions. Keep them in your head; each later chapter answers them for one more platform.

QuestionWhat can differ
1. How do I log in?user and password, enable secret, a different password per device
2. How is paging switched off?terminal length 0, set cli screen-length 0, terminal pager 0
3. Where is config mode?configure terminal, configure, configure session NAME, none at all (FortiOS)
4. How does config become live?immediately (IOS, NX-OS), on commit (Junos, PAN-OS, EOS sessions)
5. How is it saved?write memory, copy running-config startup-config, or nothing to do (Junos)

Worked example. The same job, "show me the interfaces that have an IP address", is show ip interface brief on IOS XE, NX-OS and EOS, but show interfaces terse on Junos. The IOS answer comes back with the column proto, the EOS answer with protocol. Your collector must hide those differences so that the final table looks the same for all four. That is what you build in Lab 1.

Prerequisites

  • auto-ssh and py-textfsm: you already connected with netmiko (nk_ios) and parsed output with TextFSM. Here they come back only as short refreshers.
  • py-concurrency: threads and ThreadPoolExecutor.
  • py-secrets and py-tools: environment variables, .env files, logging, exit codes.
  • Linux shell basics, and the ability to read a Python traceback.

The lab network and the two labs

DevicePlatformIPLogin
AUTO01Ubuntu server10.99.0.60netadmin / Deccan@123
R1IOS XE (PUN-CORE-R1)10.99.0.1netops / NK@2026, enable NK@2026
J1Junos10.99.0.11netops / NK@2026
E1Arista EOS10.99.0.21netops / NK@2026, enable NK@2026
N1NX-OS10.99.0.31netops / NKlab@123

Lab 1, "One script, four vendors", builds an inventory-driven collector step by step: an inventory file, a probe, an interface table, config backups, threads with retries and an alert, and a diff against yesterday. Lab 2, "Four collectors that work on Cisco and fail everywhere else", hands you four broken scripts to fix. Chapter 9 walks through it.

In the lab vs on real gear. The lab speaks no real SSH: the "SSH server" is the simulated device. Waits run on a virtual clock, so a 30-second timeout does not cost you 30 real seconds. Your scripts use from netmiko import ConnectHandler exactly as on real gear; import nkmiko is the lab's own name for the same library. When the lab differs from a real device, a note like this one says so.

Common mistakes. Believing that a script tested on one Cisco router is tested. Copying the Cisco device_type to every row of the inventory. Putting passwords in the code. Reading "no exception" as "it worked".

Exam trap. The lab names nk_ios, nk_junos and so on exist only in the lab. On real gear and in interviews you say cisco_ios, juniper_junos, arista_eos, cisco_nxos.

The nightly backup that silently skipped Junos

A team ran a backup script written on Cisco routers. It looped over fifty devices, and every device that was not Cisco got the same cisco_ios type. The Cisco backups worked; the Juniper boxes timed out and were skipped by a bare except. Nobody noticed for a month, until a Junos router died and there was no config to restore.

Lesson: the device type belongs in the inventory, per device, and every failure must be counted and reported.

"How would you automate backups across Cisco, Juniper and Arista with Python?"

I would keep an inventory file with host and netmiko device_type per device, take credentials from the environment, loop with ConnectHandler, choose the show command from a per-platform map, save one dated file per device, run devices in a thread pool with one connection per thread, retry only timeouts, write failures to a log and exit non-zero so monitoring notices.

Key takeaways

  • One collector can serve many vendors if the vendor differences live in data (inventory, command maps), not in copy-pasted scripts.
  • Five questions separate the vendors: login, paging, config mode, commit, save.
  • Use the real netmiko names; nk_* names are lab aliases.
  • Count and report failures; an exit code of 0 only means "no exception".

Upgrade to keep reading

The free plan lets you read chapter 1 of this module. Upgrade to open the remaining chapters.

  1. The device_type map and the dev dictionary
  2. Per-vendor session behaviour: prompts, paging and enable
  3. Show commands: send_command, send_command_timing and expect_string
  4. Structured output with ntc-templates on four vendors
  5. Configuration on every vendor: mode, commit and save
  6. One inventory, many vendors: collector design, threads and retries
  7. Session logs, diff against yesterday and alerting
  8. Troubleshooting a multivendor script: read the error, find the layer
  9. Summary, checklist and cheat-sheet

Join the community