我在处理Paramiko中的超时错误时遇到问题。下面与我用来连接的内容类似。
try:
dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect(IP, username, password)
stdin, stdout, stderr = dssh.exec_command('sho run')
status = 'success'
except paramiko.AuthenticationException:
status = 'fail'
当主机出现故障时,我会收到如下所示的错误,并且脚本将中止。
Traceback (most recent call last):
File "ssh3.py", line 23, in <module>
dssh.connect(IP, username, password)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 296, in connect
sock.connect(addr)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 116] Connection timed out
有没有办法捕获此错误并允许脚本从头开始运行?
答案 0 :(得分:3)
当然。您只需要捕获正在引发的异常。
# At the beginning of your code:
import socket
# In your loop
try:
# Your stuff
# Replace the existing exception handler:
except (socket.error, paramiko.AuthenticationException):
status = 'fail'