python try expect continue_Python在for循环中嵌套try语句,退出one和process next for statement...

我的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语句,但是似乎无法让脚本继续,只能完全退出。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值