有没有办法在Python中不打印任何回溯错误消息
我有一些这样的代码有没有办法在Python中不打印任何回溯错误消息,python,ssh,paramiko,traceback,Python,Ssh,Paramiko,Traceback,我有一些这样的代码 import paramiko try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(IP, username=myusername,password=mypassword,timeout=3) e
import paramiko
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP, username=myusername,password=mypassword,timeout=3)
except:
print ("[-] Wrong : "+ip+" : "+username+" : "+password)
当我运行它时,它会不断地回溯SSH问题,例如:
Traceback (most recent call last):
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
我想知道是否可以不在屏幕上打印任何回溯消息?
谢谢
以下是全部错误:
Traceback (most recent call last):
File "test123.py", line 50, in function1
client.connect(ip, username=myusername, password=mypassword,timeout=3)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/client.py", line 380, in connect
look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/client.py", line 621, in _auth
raise saved_exception
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/client.py", line 608, in _auth
self._transport.auth_password(username, password)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/transport.py", line 1271, in auth_password
return self.auth_handler.wait_for_response(my_event)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/auth_handler.py", line 208, in wait_for_response
raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/work.py", line 920, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/work.py", line 868, in run
self._target(*self._args, **self._kwargs)
File "test123.py", line 56, in slaveWork
except paramiko.ssh_exception:
TypeError: catching classes that do not inherit from BaseException is not allowed
Exception: Error reading SSH protocol banner
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/transport.py", line 1888, in _check_banner
buf = self.packetizer.readline(timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/packet.py", line 331, in readline
buf += self._read_timeout(timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/paramiko/packet.py", line 498, in _read_timeout
raise EOFError()
EOFError
你可以完成你的尝试,只是做一个一般的捕获,然后什么也不做
import paramiko
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP, username=myusername,password=mypassword,timeout=3)
except Exception as e:
pass
最好的选择可能是使用该模块。
对于Python 2.x,您可以执行以下操作:
import sys
import paramiko
sys.tracebacklimit = 0
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(IP, username=myusername,password=mypassword,timeout=3)
except:
print ("[-] Wrong : "+ip+" : "+username+" : "+password)
对于Python3.5(),有些人报告说应该将其设置为None
才能工作,即:
sys.tracebacklimit = None
对于引发的多个异常,报告的某些人员不保证工作().您的
try
没有expect
。如果除了paramiko.ssh\u异常之外您还有。SSHException:
而不是您现在拥有的。您是否在您提供的链接中尝试了sys.tracebacklimit=0
或sys.tracebacklimit=None
“当设置为0或更小时,所有回溯信息将被抑制,并且只打印异常类型和值。”。在这种情况下,我仍然会收到一个错误,但我只会收到一份简历。