run cli command with Netmiko




csv files
Script
script
#!/usr/bin/env python3
from netmiko import ConnectHandler
from datetime import datetime
import csv

#Variable
i = 0
ip_list = []

# To calculate the time the script take
now1 = datetime.now()
print("Start:",now1)

# Dictionary of Devices loging information
juniper_junos_dict = {
    'device_type': 'juniper',
    'ip': "172.30.95.177",
    'username': "Netbox",
    'password': "Netbox"
}

#open the csv file
with open('list_of_Devices.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for lines in csv_reader:
        ip_list.append(lines[0])


# Loop thru all the Devices
for ip_addr in ip_list:
	# global_delay_factor IS THE KEY :p
#	net_connect = ConnectHandler(**juniper_junos, global_delay_factor=1)
	try:
		now2a = datetime.now()
		net_connect = ConnectHandler(device_type= 'juniper',host= ip_addr,username= "Netbox",password= "Netbox")
		now2b = datetime.now()
		#print("After Connecttion:",now2b)
		print(ip_addr," responsed in ", now2b-now2a)
	except:
		now2b = datetime.now()
		print("No response from: ",ip_addr ," in ", now2b-now2a)

	#run the command and write to file
	output = net_connect.send_command('show chassi hardware | display xml ')
#	f = open(juniper_junos["ip"]+".xml", "w")
	f = open(ip_addr+".xml", "w")
	f.write(output)
	f.close()
	#print(output)

now3 = datetime.now()

print("Start:",now1)
#print("After Connecttion:",now2)
print("End:",now3)
#print("Connection time:",now2-now1)
print("Total= Connection + run commands + write files:",now3-now1," for :", len(ip_list)," sessions.")