I have the following script that is used to log into Cisco devices and make config changes. The script seems ok except for the very end where the IDE complains that the "continue is not properly in loop". I want this script to be able to continue if it hits an exception. I am hitting a road block.
Thanks!
from getpass import getpass
from netmiko import ConnectHandler
import time
import sys
import traceback
username = input('Username:')
password = getpass()
with open('commands_file') as f:
commands_list = f.read().splitlines()
with open('device_file') as f:
devices_list = f.read().splitlines()
with open("exception_log.txt", "w") as log:
for devices in devices_list:
print ('Connecting to device ' + devices)
device_ip = devices
ios_device = {
'device_type': 'cisco_ios',
'ip': device_ip,
'username': username,
'password': password
}
try:
net_connect = ConnectHandler(**ios_device)
net_connect.save_config()
time.sleep(5)
output = net_connect.send_command_timing("reload in 10")
if 'Proceed with reload?' in output:
output += net_connect.send_command_timing("y")
print(output)
output = net_connect.send_config_set(commands_list)
print(output)
time.sleep(15)
except:
print("An Exception Occurred")
traceback.print_exc(file=log)
time.sleep(15)
continue
Looks like an indentation error to me, try indenting everything starting from try:
, because it seems the try-except blocks are not within the loop.