我的Python在学习这个。在
有一个脚本连接到从文件中提供的地址列表,并使用pexpect对每个地址执行操作。我的问题是,如果有一个登录错误,我希望它在将异常错误发送到日志后,继续移动到下一个地址。它所做的是将父try语句继续到最后,这是毫无意义的,因为脚本无论如何都无法连接:for ip in ips:
try:
try:
#print curent address
print "Connecting to " + ip
#Login section
c = pexpect.spawn('ssh -o StrictHostKeyChecking=no %s@%s' % (tacacsUser, ip))
c.timeout = 10
c.expect('password:')
#Login to the device
c.expect('#')
except Exception, e: #Write login errors to file
print "Login error while connecting to " + ip
saveLogs('LoginErrors.txt', e, 'Error connecting to ')
#send commands and such
except Exception, e:
print "Error occured while processing commands for " + ip
saveLogs('ConfigErrors.txt', e, 'Error occured while processing ')
如果嵌套try命中异常,我希望它一直移回for循环并从下一个地址开始。在
我一直在研究这个嵌套的exit语句,但是似乎无法让脚本继续,只能完全退出。在