06-30-2020 04:29 PM
Hi lads,
i am trying to backup Cisco switches with python script. First i do ssh then I have a separate text file where i collected all IP addresses and want to loop one by one using dictionary. But it only chooses first IP addresses and then script stops. I am so new in Python and i am sure i am doing something wrong. See the code below. Please correct me where i am doing wrong.
see code below
Thanks,
Aslan
#Import script dependencies
from netmiko import ConnectHandler
import getpass
import logging
#Enable logging
logging.basicConfig(filename='debug.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")
#Read from a list of hostnames to connect to
switches = open('hosts.txt', 'r')
switches = switches.readline()
switches = switches.strip().splitlines()
#Get Username and password from input
userName = input('Username: ')
passWord = getpass.getpass()
#Loop to process switches in hosts.txt file
Cisco_Catalyst = {}
for host in switches:
Cisco_Catalyst.update({
'device_type': 'cisco_ios',
'ip': host,
'username': userName,
'password': passWord,
})
#Netmiko SSH Connection Handler
net_connect = ConnectHandler(**Cisco_Catalyst)
#open file to write command output
file = open(host + '_backup.txt', 'w')
#Exectute 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()
07-01-2020 03:02 PM
Hi there,
This is an issue with indentation. You enter the for loop and create the Cisco_Catalyst dictionary for each switch, then exit. It then proceeds with the Netmiko connection, I'd have thought on the last IP address in your switches file. Simply indent the code beneath the Cisco_Catalyst.update() section:
#Import script dependencies from netmiko import ConnectHandler import getpass import logging #Enable logging logging.basicConfig(filename='debug.log', level=logging.DEBUG) logger = logging.getLogger("netmiko") #Read from a list of hostnames to connect to switches = open('hosts.txt', 'r') switches = switches.readline() switches = switches.strip().splitlines() #Get Username and password from input userName = input('Username: ') passWord = getpass.getpass() #Loop to process switches in hosts.txt file Cisco_Catalyst = {} for host in switches: Cisco_Catalyst.update({'device_type': 'cisco_ios', 'ip': host, 'username': userName, 'password': passWord })
#Netmiko SSH Connection Handler net_connect = ConnectHandler(**Cisco_Catalyst) #open file to write command output file = open(host + '_backup.txt', 'w') #Exectute 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()
cheers,
Seb.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide