管理ssh协议横幅错误

时间:2017-10-22 15:21:52

标签: python exception ssh paramiko

我使用Paramiko Python模块编写一个简单的多个ssh连接throw:

import paramiko
import time
i=0
while i<20 :
     ssh = paramiko.SSHClient()
     starttime=time.clock()
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     try:
        ssh.connect('127.0.0.1', username="user", password="password")
     except:
        endtime=time.clock()
     except OSError:
         print("Os error")
     except paramiko.SSHException:
         print("Ssh error")
     total=endtime-starttime
     print(total)
     i=i+1

问题是代码没有捕获所有引发的异常,即:

Exception: Error reading SSH protocol banner[Errno 104] Connection reset by peer
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2000, in _check_banner
buf = self.packetizer.readline(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 353, in readline
buf += self._read_timeout(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 531, in _read_timeout
x = self.__socket.recv(128)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1854, in run
self._check_banner()
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2005, in _check_banner
'Error reading SSH protocol banner' + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 104] Connection reset by peer

Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2000, in _check_banner
buf = self.packetizer.readline(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 353, in readline
buf += self._read_timeout(timeout)
File "/usr/local/lib/python3.5/dist-packages/paramiko/packet.py", line 533, in _read_timeout
raise EOFError()
EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 1854, in run
self._check_banner()
File "/usr/local/lib/python3.5/dist-packages/paramiko/transport.py", line 2005, in _check_banner
'Error reading SSH protocol banner' + str(e)
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

我会捕获所有引发的异常,以便完全控制我的标准输出。

0 个答案:

没有答案