我有一个代码,其中im在hosts列表中循环并将连接附加到connections列表,如果有连接错误,我想跳过这个并继续hosts列表中的下一个主机。
我现在拥有的是:
def do_connect(self):
"""Connect to all hosts in the hosts list"""
for host in self.hosts:
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host['ip'], port=int(host['port']), username=host['user'], timeout=2)
except:
pass
#client.connect(host['ip'], port=int(host['port']), username=host['user'], password=host['passwd'])
finally:
if paramiko.SSHException():
pass
else:
self.connections.append(client)
这不能正常工作,如果连接失败,它只是一次又一次地循环同一个主机,直到它建立连接,我如何解决这个问题?
好的,开始工作了,我需要添加由Mark提到的Continue,还有之前的if check inside最后总是返回true,所以也被修复了。
下面是固定代码,它不会添加失败的连接,并在之后正常继续循环:
你自己的回答在很多方面还是错的。。。
相关问题 更多 >
编程相关推荐