Pull and Push configurations

This is an easy exemple of how to send a show command to multiple devices, this exemple is base on ‘cisco_ios’ device_type so for other like cisco_xr  and juniper_os dont forget the send_command.commit()

from netmiko import Netmiko
from getpass import getpass

password = getpass()

csr1 = { ‘host’: ‘define_ip’, ‘username’: ‘user’, ‘password’: ‘pass’, ‘device_type’: ‘os_code’ }
csr2 = { ‘host’: ‘define_ip’, ‘username’: ‘user, ‘password’: ‘pass’, ‘device_type’: ‘os_code’ }
csr3 = { ‘host’: ‘define_ip’, ‘username’: ‘user’, ‘password’: ‘pass’, ‘device_type’: ‘os_code’ }

devices = [csr1, csr2, csr3]

for device in devices:
….net_conn = Netmiko(**device)

….show_run = net_conn.send_command(“show run”)
….show_arp = net_conn.send_command(“show arp”)
….show_ip_int = net_conn.send_command(“show ip int br”)

….output = net_conn.send_command(“show arp”)
….print(“*” * 79)
….print(“This is the arp table for {}”.format(device[‘host’]))
….print(“*” * 79)
….output = net_conn.send_command(“show ip int br”)
….print(“This is the interface brief for {}”.format(device[‘host’]))
….with open(“show_run.{}.txt”.format(device[‘host’]), “a”) as f:
…..with open(“show_arp.{}.txt”.format(device[‘host’]), “a”) as f:
….with open(“show_ip_int.{}.txt”.format(device[‘host’]), “a”) as f:
….print(“Files have been saved”)
print(“End of Script”)

more will come to later how to add the date to the backup configuration since a truly believe that anytime pushing any_config  a backup should be done and a template should be generated for a rolling back configuration. Still in Learning process !

send config from files exemple :

net_connect = ConnectHandler(**device)

pre_check = net_connect.send_config_from_file(config_file=”pre_check.txt”)
roll_out = net_connect.send_config_from_file(config_file=”roll_out.txt”)
post_check = net_connect.send_config_from_file(config_file=”post_check.txt”)

with open(“pre_check.{}.txt”.format(device[‘host’]), “a”) as f:

with open(“roll_out.{}.txt”.format(device[‘host’]), “a”) as f:

with open(“post_check.{}.txt”.format(device[‘host’]), “a”) as f:



From a Host File :

from netmiko import ConnectHandler
import getpass

# Read from a list of hostnames to connect to
hosts = open(‘hosts.txt’,’r’)
hosts = hosts.read()
hosts = hosts.strip().splitlines()

# Get UserName and password from input
userName = input(‘Username: ‘)
passWord = getpass.getpass()

# Loop to process hosts in hosts.txt file
for host in hosts:
# Define device type and connection attributes
devices = {
‘device_type’: ‘cisco_ios’,
‘ip’: host,
‘username’: userName,
‘password’: passWord),

# Netmiko SSH Connection Handler
net_connect = ConnectHandler(**devices)

#open file to write command output
file = open(host + ‘_output.txt’, ‘w’)

# Execute commands
output = net_connect.send_command(‘skip-page-display’)
output = net_connect.send_command(‘show run’)

# Print output to console screen
print(‘————– Output from ‘ + host + ‘——————‘)

# Write output to file above

Another example using send_config_set :

import os
from netmiko import Netmiko
from getpass import getpass
from pprint import pprint
from datetime import datetime

#password = getpass()
devices = {‘host’: ‘xxxxx’, ‘username’: ‘xxxxx’, ‘password’: ‘xxxx’, ‘device_type’: ‘cisco_xe’, ‘session_log’: ‘logging.txt’, ‘fast_cli:True’}

ssh_conn = Netmiko(**devices)

commands = [‘ip name-server’]
verify_command = “ping google.com”

start_time = datetime.now()
push1 = ssh_conn.send_config_set(commands)
push2 = ssh_conn.send_command(verify_command)

if “!!” in push2:
print(“Ping Successful:”)
print(f”Result :{push2}”)
print(“Not Working FIX IT”)

end_time = datetime.now()
print(f”Execution Time for delay of 2 :{end_time} : {start_time}”)


References :
Python for Network Engineers


Leave a Comment