Configuring network devices using Python

Configuring network devices using Python can be done using several libraries such as Netmiko, Napalm, and Paramiko. Here is an example of using Netmiko to configure a Cisco IOS device:

from netmiko import ConnectHandler

device = {
    'device_type': 'cisco_ios',
    'ip': '',
    'username': 'admin',
    'password': 'password'

connection = ConnectHandler(**device)
config_commands = ['interface Loopback0', 'ip address']
output = connection.send_config_set(config_commands)



In this example, we use the ConnectHandler class from the netmiko library to connect to a Cisco IOS device. We then define a list of configuration commands and use the send_config_set function to send these commands to the device. Finally, we print the output returned by the function.

The send_config_set function takes a list of commands as an argument and sends them to the device. The output returned by the function contains the device’s response to the configuration commands.

Note that you can also use the send_config_from_file function to send configuration commands stored in a file to the device.

Another library for configuring network devices is Napalm, which provides a vendor-agnostic interface to configure devices from multiple vendors. Here is an example of using Napalm to configure a Cisco IOS device:

from napalm import get_network_driver

driver = get_network_driver('ios')
device = {
    'hostname': '',
    'username': 'admin',
    'password': 'password',
    'optional_args': {'secret': 'enable_password'}

connection = driver(**device)

config_commands = ['interface Loopback0', 'ip address']
diff = connection.compare_config()
if diff:


In this example, we use the get_network_driver function to load the IOS driver from Napalm. We then create a dictionary with the device information and use it to create a driver instance. We connect to the device using the open method and define a list of configuration commands.

We use the load_merge_candidate method to load the configuration commands into the device’s candidate configuration. We then use the compare_config method to compare the candidate configuration with the running configuration. If there are any differences, we use the commit_config method to apply the changes. Otherwise, we use the discard_config method to discard the changes.

Configuring network devices with Python can be a powerful way to automate repetitive and error-prone tasks. By using libraries like Netmiko and Napalm, you can create reusable code that can help you to manage your network infrastructure more efficiently.

Leave a Reply