Netmiko Configuration Commands Example

Netmiko Configuration Commands Example
In: Netmiko Python NetDevOps

In this blog post, we're going to look at how to use Netmiko to send configuration commands to network devices. Before, we talked a lot about using Netmiko for 'show commands'. But this time, it's all about configuration commands. If you're new to Netmiko or need a quick refresher, make sure to check out my introductory Netmiko blog post here. It'll give you a good starting point.

Sending a Single Configuration Command

First, we're starting with the basics by sending a single configuration command using Netmiko. To keep things simple, we'll configure an NTP server on a device. The command we use for this is ntp server x.x.x.x, where x.x.x.x is the IP address of the NTP server. Here's a quick run-through of how to do this with Netmiko.

from netmiko import ConnectHandler

username = 'admin'
password = 'Cisco123'

device = {
        "device_type": "cisco_ios",
        "host": '10.10.10.10',
        "username": username,
        "password": password,
        "secret": password
}

commands = ['ntp server 1.1.1.1']
connection = ConnectHandler(**device)
output = connection.send_config_set(commands)
print(output)

connection.disconnect()

We begin by importing ConnectHandler from Netmiko and then specifiy the login credentials. It's important to note that keeping passwords in plain text, as shown here, is not a good practice for production environments. To keep your credentials secure, there are several strategies you can employ, which I've covered in another blog post.

In our example, we define the device's IP address, the device type (in this case, cisco_ios for a Cisco IOS device), and the login credentials. We also use the same password for the secret parameter, which is often needed for entering privileged EXEC mode on Cisco devices.

Once we have our device details set up, we specify the configuration command we want to send to the device in a list. Here, our command is ['ntp server 1.1.1.1']. We then establish a connection to our device using ConnectHandler and pass it our device details. With the connection established, we send our configuration command using the send_config_set method. This method takes our list of commands and applies them to the device.

Here is the output where you can see the send_config_set method automatically enters configure terminal mode, execute the command and then exits from the config mode.

configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
c9300(config)#ntp server 1.1.1.1
c9300(config)#end
c9300#

Sending Multiple Commands

In our second example, we're going to look at how to send multiple configuration commands at once using Netmiko. This time, we'll remove the NTP server we configured in our previous example and set up two new ones instead.

from netmiko import ConnectHandler

username = 'admin'
password = 'Cisco123'

device = {
        "device_type": "cisco_ios",
        "host": '10.10.10.10',
        "username": username,
        "password": password,
        "secret": password
}

commands = ['no ntp server 1.1.1.1', 'ntp server 8.8.8.8', 'ntp server 8.8.4.4']
connection = ConnectHandler(**device)
output = connection.send_config_set(commands)
print(output)

connection.disconnect()

The key difference in this example is the list of commands we're sending to the device. Instead of just adding an NTP server, we start by removing the previously configured server with no ntp server 1.1.1.1. After that, we add two new NTP servers with ntp server 8.8.8.8 and ntp server 8.8.4.4. This demonstrates how you can send a series of commands to make multiple changes in one go. Here is the output when you run the script.

configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
c9300(config)#no ntp server 1.1.1.1
c9300(config)#ntp server 8.8.8.8
c9300(config)#ntp server 8.8.4.4
c9300(config)#end
c9300#

Interface Configurations

In this final example, we're configuring multiple interfaces at once. Instead of working with global configuration commands like in the previous examples, here we focus on interface-specific configurations. We have a list of interfaces, namely ['Te1/0/1', 'Te1/0/2', 'Te1/0/3'], that we plan to configure.

For each interface in our list, we're setting a description to JUST-TESTING and bringing the interface up by using the no shut command. This is done within a loop that iterates through each interface in our list.

from netmiko import ConnectHandler

username = 'admin'
password = 'Cisco123'

interfaces = ['Te1/0/1', 'Te1/0/2', 'Te1/0/3']

device = {
        "device_type": "cisco_ios",
        "host": '10.10.10.10',
        "username": username,
        "password": password,
        "secret": password
}

connection = ConnectHandler(**device)
for int in interfaces:
    commands = [f'interface {int}', 'description JUST-TESTING', 'no shut']
    output = connection.send_config_set(commands)
    print(output)

connection.disconnect()

The commands change slightly from the previous examples because now we dynamically construct the command list based on the interface we're currently configuring. This approach allows us to efficiently apply the same set of configuration commands (description and no shut) to multiple interfaces in a row. Here is the output when running the script.

configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
c9300(config)#interface Te1/0/1
c9300(config-if)#description JUST-TESTING
c9300(config-if)#no shut
c9300(config-if)#end
c9300#
configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
c9300(config)#interface Te1/0/2
c9300(config-if)#description JUST-TESTING
c9300(config-if)#no shut
c9300(config-if)#end
c9300#
configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
c9300(config)#interface Te1/0/3
c9300(config-if)#description JUST-TESTING
c9300(config-if)#no shut
c9300(config-if)#end
c9300#

Closing Up

I hope these examples have given you a good starting point for your own automation projects. Whether you're just getting started with Netmiko or looking to expand your automation toolkit, there's always something new to learn and apply. Happy automating!

Table of Contents
Written by
Suresh Vina
Tech enthusiast sharing Networking, Cloud & Automation insights. Join me in a welcoming space to learn & grow with simplicity and practicality.
Comments
More from Packetswitch
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Packetswitch.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.