Free tech learning for everyone, from basics to pro

Network AutomationProgramming and Application SecurityIntermediate

Netmiko in depth: connecting to many vendors, sending commands and config

In short: Netmiko is a Python library that simplifies SSH to network devices. It understands prompts, paging and configuration mode for many vendors. You use ConnectHandler to log in, send_command for show commands and send_config_set for changes, with credentials loaded securely and errors handled per device.

What Netmiko solves

Raw SSH libraries give you a byte stream. Network CLIs, however, have prompts that change, "--More--" paging, configuration modes and slow responses. Netmiko is an open-source Python library built on the Paramiko SSH library that deals with all of this for you. You choose a device_type and Netmiko knows how to log in, disable paging, enter config mode and detect when a command has finished.

Where Netmiko sitsYour scriptloops, logic, reportsNetmikoprompts, paging, config modeParamikoSSH librarySSH sessionencrypted, TCP 22Device CLIIOS, NX-OS, EOS, Junos
Figure 1. Netmiko adds device-aware handling on top of the Paramiko SSH library.

Install it in a virtual environment with pip install netmiko. Common device types include cisco_ios, cisco_xe, cisco_nxos, arista_eos, juniper_junos and linux. If you are unsure, SSHDetect can guess the type, but naming it explicitly is faster and more reliable.

Connecting and running show commands

Keep credentials out of the script. Read them from environment variables or prompt with getpass. Use the connection as a context manager so it always disconnects.

import os
from getpass import getpass
from netmiko import ConnectHandler

device = {
    "device_type": "cisco_ios",
    "host": "192.168.10.11",
    "username": os.environ.get("NET_USER") or input("Username: "),
    "password": os.environ.get("NET_PASS") or getpass("Password: "),
}

with ConnectHandler(**device) as conn:
    print(conn.find_prompt())
    output = conn.send_command("show ip interface brief")
    print(output)
    parsed = conn.send_command("show version", use_textfsm=True)
    print(type(parsed))

With use_textfsm=True, Netmiko uses the ntc-templates collection (installed with Netmiko 4 and later) to return a list of dictionaries instead of text, when a template exists for that command and platform. If not, you get plain text back, so check the type before using it.

Sending configuration

A safe change with NetmikoConnectConnectHandlerCheck beforeshow commandsPush configsend_config_setVerifyshow againSavesave_config
Figure 2. A Netmiko change should always check state before and after, then save.

send_config_set() enters configuration mode, sends a list of lines and exits again. send_config_from_file() does the same from a text file, which pairs well with Jinja2 templates. On Cisco IOS, save_config() writes the running configuration to startup.

import os
from netmiko import ConnectHandler

commands = [
    "vlan 30",
    " name GUEST",
    "interface GigabitEthernet1/0/10",
    " switchport access vlan 30",
]

with ConnectHandler(device_type="cisco_ios", host="192.168.10.11",
                    username=os.environ["NET_USER"],
                    password=os.environ["NET_PASS"]) as conn:
    before = conn.send_command("show vlan brief")
    result = conn.send_config_set(commands)
    print(result)
    after = conn.send_command("show vlan brief")
    if "GUEST" in after:
        conn.save_config()
    else:
        print("VLAN not found after change, not saving")

Tip: slow commands such as show tech-support may exceed the default wait. Pass read_timeout=120 to send_command() rather than adding sleeps.

Scaling up and staying safe

  • Many devices: loop over an inventory and catch NetmikoTimeoutException and NetmikoAuthenticationException per device. For tens or hundreds of devices, run in parallel with Nornir or concurrent.futures.
  • Jump hosts: Netmiko can use an SSH config file (ssh_config_file) to reach devices through a bastion.
  • Host keys: in production, verify SSH host keys rather than accepting any key, so you do not send credentials to an impostor.
  • Least privilege: use a dedicated automation account with only the privilege level it needs, and log its actions with AAA (authentication, authorisation and accounting).
  • Change control: test in a lab, get approval, and run changes in a maintenance window with a rollback plan.
MethodUse it to
send_command()Run one show command and wait for the prompt
send_config_set()Push a list of configuration lines
send_config_from_file()Push lines from a rendered file
save_config()Save running configuration
find_prompt()Confirm you are on the expected device

Practice

Why does Netmiko need a device_type?
Each platform has different prompts, paging commands and configuration mode behaviour. The device type tells Netmiko which rules to apply.
What does use_textfsm=True return when no template matches?
The plain text output, so your script should check whether it received a list or a string.
Name two ways to keep passwords out of a Netmiko script.
Read them from environment variables, prompt with getpass, or fetch them from a secrets vault at run time.

Key takeaways

  • Netmiko handles prompts, paging and config mode over SSH for many vendors.
  • send_command for show output; send_config_set or send_config_from_file for changes.
  • use_textfsm=True can return structured data when a template exists.
  • Load credentials securely, verify host keys and use a least-privilege account.
  • Check before and after every change, and only then save.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.