Python Netmiko Basics

Learn how to use the Netmiko Python library for automating network changes over SSH.

Python Netmiko Basics

Introduction to Network Automation with Python

Python has become the lingua franca of network automation. One of the most popular and accessible libraries for interacting with traditional network devices via CLI is Netmiko.

What is Netmiko?

Netmiko is a multi-vendor Python library that simplifies SSH connections to network devices. It handles the complexities of dealing with different device prompts, paging (e.g., the “—More—” prompt), and timing issues, allowing you to focus on sending commands and parsing the output.

Key Concepts and Usage

1. Connection Dictionaries

To connect to a device, you define a dictionary containing the connection parameters.

cisco_device = {
    'device_type': 'cisco_ios',
    'host':   '192.168.1.10',
    'username': 'admin',
    'password': 'password123',
}

2. Establishing a Connection

Use the ConnectHandler to initiate the SSH session.

from netmiko import ConnectHandler

net_connect = ConnectHandler(**cisco_device)

3. Sending Commands

  • send_command(): Used for sending “show” commands and retrieving the output.
  • send_config_set(): Used for sending configuration commands. Netmiko automatically enters and exits configuration mode.