netmiko-featured-image

Python: Start Automating with Netmiko

Built on Paramiko, Netmiko is a popular python library that can be used to interface with a wide range of devices programmatically. Like many others, my introduction to network automation and programmability was founded on the need to touch many devices in a short amount of time, and that is when I discovered Netmiko. Do you have tens or even hundreds of devices that you need to run show commands, or make configuration changes on? Netmiko makes that easy with a minimal amount of Python code.

This post is an overview of how to get started with Netmiko to automate your network. However, this demo is only a fraction of what Netmiko is capable of. I encourage you to check out Netmiko’s GitHub page and documentation to learn more about how you can fully utilize the tool.

I have created a GitHub repository to pair with this article, which can be found here. Feel free to clone it and follow along. If you already have your own devices and want to build your own scripts to follow along with, feel free!

Note: I am using an Ubuntu virtual machine and Cisco’s always-on IOS-XE sandbox for this demonstration.

Getting Started

Assuming you already have Python installed and you are using Linux, Netmiko can be installed with Pip. You can refer to the pip user guide for more information on how to use pip on other operating systems.

pip install netmiko

Netmiko Basics

As mentioned above, I will be utilizing Cisco’s always-on IOS-XE sandbox for this demo. Cisco has many sandboxes available on their DevNet website which makes it easy to play with and learn new tools. The current SSH information is below:

CSR1000V Host: sandbox-iosxe-latest-1.cisco.com
SSH Port: 22
Username: developer
Password: C1sco12345

Note: The above information could change at any moment. If it is not working, you may need to check the sandbox itself for the latest credentials.

Import Netmiko

The first thing we’ll want to do is import Netmiko’s ConnectHandler. This is what we’ll use to create our SSH connection to a device.

from Netmiko import ConnectHandler
Create a Device Dictionary

The next thing we want to do is create a device dictionary. This is a python dictionary that stores information for a device such as it’s type, SSH credentials, and SSH port. You can name the dictionary whatever you like, I named mine “ios_xe”.

  • device_type: The type of device you’re connecting to. Netmiko supports many device types (IOS, IOS-XE, NX-OS, Juniper, etc). I am using cisco_xe here since I am connecting to an IOS-XE device. If you’re connecting to an older IOS device, it would be cisco_ios. Refer to the documentation for more information.
  • host: The IP or hostname of the device
  • username: SSH Username
  • password: SSH Password
  • port: Defaults to 22, but you can specify the port here if it is not 22.
  • session_log: Logs the SSH session. This is an optional parameter, but I find it to be useful. It will generate a file with the name provided. If you’re using my code samples, you can remove this option to stop the logging

If you have multiple devices, you could create multiple device dictionaries and loop through them to connect to each device. That capability is not covered here, but you can find an example of that and many other capabilities on Netmiko’s GitHub page.

ios_xe = {
    "device_type": "cisco_xe",
    "host": "sandbox-iosxe-latest-1.cisco.com",
    "username": "developer",
    "password": "C1sco12345",
    "port": 22,
    "session_log": "iosxe_log.log" [optional]
}
Connect

To create an SSH connection, we need to pass our device dictionary into the ConnectHandler and set it as a new object. Netmiko uses “net_connect” as the name in the documentation, which I mimic’d here for ease of use.

net_connect = ConnectHander(**ios_xe)

Netmiko also supports the use of context managers, seen below. What’s great about using context managers with Netmiko is that once your code is finished running, it will automatically use the disconnect method to close the SSH session with the device.

with ConnectHandler(**ios_xe) as net_connect:
    # Code here
Running Commands

Once we have our net_connect object, we’re ready to use Netmiko’s methods to interact with our device. Netmiko has several options for sending commands:

  • send_command(): Send a single command and return output
  • send_command_timing(): Same as send_command, but timing based
  • send_config_set(): Send a single configuration command, or a list of commands
  • send_config_from_file(): Send configuration commands from a file

In the sample code, I use send_command() to send the show ip int brief command and store the command output as output, which is then printed out to the terminal.

output = net_connect.send_command("show ip int brief")
print(output)

To send configuration commands, you can either use the send_config_set() method, or the send_config_from_file() method. send_config_set() takes in a list as an argument that can contain a single, or multiple commands.

I’m going to use both to create loopback510 and loopback511 respectively, give them IP addresses and verify that they have been created by sending “show ip int brief” with the send_command() function. I’ll also be sure to clean up my config at the end of each script.

send_config_set():


# Create list of commands
config_commands = ["int loopback510", "ip address 5.10.10.10 255.255.255.255"]

# Run config commands
net_connect.send_config_set(config_commands)

# Run "show ip int brief" to verify
output = net_connect.send_command("show ip int brief")
print(output)

# Always clean up your config!
cleanup_commands = ["no int loopback510"]
net_connect.send_config_set(cleanup_commands)

send_config_from_file():

# Run config commands from file
net_connect.send_config_from_file("commands.txt")

# Run "show ip int brief" to verify
output = net_connect.send_command("show ip int brief")
print(output)

# Always clean up your config!
cleanup_commands = ["no int loopback511"]
net_connect.send_config_set(cleanup_commands)
Disconnecting

Netmiko also has the disconnect() function, which does exactly what it implies. It disconnects the SSH session. This isn’t REALLY necessary since SSH sessions will timeout, but I find that it is cleaner to go ahead and disconnect the session explicitly when you’re finished interacting with the device. If you’re using a context manager, there’s no need to call the disconnect method.

net_connect.disconnect()

Other Useful Methods

There are also several other methods, outside of the ones discussed above, you may find to be useful:

find_prompt(): Can be used to locate the prompt of the device. This is useful if you want to bring in the name of the device into your code dynamically.

prompt = net_connect.findprompt()
print(prompt)

Prints:
csr1000v-1#

enable(): Used to enter enable mode
save_config(): Copies running config to startup config

Tons of Possibilties!

I hope this post has given you some ideas on how you can incorporate Netmiko and Python into your network. Although I did not cover advanced ways to use these tools, the possibilities are endless. Need to make config changes across your network? You can do it with one Python script. Need to take configuration backups, retrieve statistics, or get the state of your network? Netmiko can do it!

Scroll to Top