How to Back Up Your Network Config in 15 Lines of Code with Netmiko

How to Back Up Your Network Config in 15 Lines of Code with Netmiko

When it comes to network automation we are spoilt with the plethora of tools available to us. One tool that deserves its space within any network automation toolbox is Netmiko.

Netmiko is a multi-vendor Python SSH library that simplifies the process of configuring and obtaining data from your network devices.

In short, Netmiko provides support for a ton of devices, a low barrier of entry for learning, and various built-in features such as structured parsing. Therefore you can get some great automation wins - QUICK!

For example below shows you how to backup a device config with less than 15 lines of code:

# Install
$ pip3 install netmiko

# Create ConnectHandler
from netmiko import ConnectHandler

net_connect_ios = ConnectHandler(
    {
        "device_type": "cisco_ios",
        "host": "lab.packetflow.co.uk",
        "username": "username",
        "password": "password",
    }
)

# Collect config
output = net_connect_ios.send_command("show run")

# Write to file
with open("device-backup.txt", "w") as opened_file:
    opened_file.write(output)

Subscribe to our newsletter to keep updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!