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)
….net_conn.enable()
….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(output)
….print(“*” * 79)
….output = net_conn.send_command(“show ip int br”)
….print(“This is the interface brief for {}”.format(device[‘host’]))
….print(output)
….with open(“show_run.{}.txt”.format(device[‘host’]), “a”) as f:
……..f.write(show_run)
…..with open(“show_arp.{}.txt”.format(device[‘host’]), “a”) as f:
……..f.write(show_arp)
….with open(“show_ip_int.{}.txt”.format(device[‘host’]), “a”) as f:
……..f.write(show_ip_int)
….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)
print(net_connect.find_prompt())
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:
f.write(pre_check)
with open(“roll_out.{}.txt”.format(device[‘host’]), “a”) as f:
f.write(roll_out)
with open(“post_check.{}.txt”.format(device[‘host’]), “a”) as f:
f.write(post_check)
net_connect.disconnect()
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 + ‘——————‘)
print(output)
print()
print()
# Write output to file above
file.write(output)
file.close()
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)
print(ssh_conn.find_prompt())
commands = [‘ip name-server 1.1.1.2’]
verify_command = “ping google.com”
start_time = datetime.now()
push1 = ssh_conn.send_config_set(commands)
print(push1)
push2 = ssh_conn.send_command(verify_command)
if “!!” in push2:
print(“Ping Successful:”)
print(f”Result :{push2}”)
else:
print(“Not Working FIX IT”)
end_time = datetime.now()
print(f”Execution Time for delay of 2 :{end_time} : {start_time}”)
References :