我正在尝试查看设备列表,其中一些设备似乎已安装SSH v1.5,即使从异常语句捕获它,我仍从paramiko收到一些异常消息。我想知道如何抑制此消息,因为它会干扰到潜在用户的输出。
我正在使用:
paramiko == 2.6.0 Python 3.6.3
我查看了一些日志记录选项,但是这些选项似乎无效。我正在使用线程和队列模块同时ssh到8个设备中以获取配置。
# Try connect via SSH:
tries = 1
ssh_error = None
retries = 3
while tries <= retries:
print("START")
try:
rc.connect(
device, port, creds['username'], creds['password'],
look_for_keys=False, allow_agent=False, timeout=5)
except paramiko.BadHostKeyException as err:
ssh_error = "Bad host key error: %s" % err
except paramiko.AuthenticationException as err:
ssh_error = "Authentication error: %s" % err
except paramiko.ssh_exception.NoValidConnectionsError as err:
ssh_error = "Unable to connect via SSH: %s" % err
except paramiko.SSHException as err:
ssh_error = "Exception: %s" % err
print("EXCEPTION CAUGHT")
except socket.error as err:
ssh_error = "Socket error: %s" % err
except OSError as err:
ssh_error = "OS error: %s" % err[p ]
except Exception as err:
ssh_error = "General exception error: %s" % err
if ssh_error is not None:
tries += 1
if tries == retries + 1:
break
print("END")
输出如下:
(venv) [user@somehost venv]$ ./connect.py
START
START
START
START
START
Exception: Incompatible version (1.5 instead of 2.0)
Traceback (most recent call last):
File "/opt/connect/venv/lib/python3.6/site-packages/paramiko/transport.py", line 2039, in run
self._check_banner()
File "/opt/connect/venv/lib/python3.6/site-packages/paramiko/transport.py", line 2240, in _check_banner
raise SSHException(msg.format(version))
paramiko.ssh_exception.SSHException: Incompatible version (1.5 instead of 2.0)
EXCEPTION CAUGHT
END
START
START
Exception: Incompatible version (1.5 instead of 2.0)
Traceback (most recent call last):
File "/opt/connect/venv/lib/python3.6/site-packages/paramiko/transport.py", line 2039, in run
self._check_banner()
File "/opt/connect/venv/lib/python3.6/site-packages/paramiko/transport.py", line 2240, in _check_banner
raise SSHException(msg.format(version))
paramiko.ssh_exception.SSHException: Incompatible version (1.5 instead of 2.0)
EXCEPTION CAUGHT
END
有8个线程在运行,因此为什么您会看到一些STARTS,但其目的是表明我实际上正在捕获异常。看起来paramiko的动作有些奇怪。在我6年以上的编码中,我实际上还没有看到过类似的东西,因此不确定是否是paramiko等的较新版本。
现在,我知道这与不支持的SSH版本有关,但是我的问题是,如何使paramiko在捕获异常并希望通过异常语句来处理它们时不显示这些警告。如果可能的话,我也不想去“破解” paramiko模块。