有没有办法不在Python中打印任何回溯错误消息

时间:2017-01-28 05:49:31

标签: 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) 
        except:
            print ("[-] Wrong : "+ip+" : "+username+" : "+password)

当我运行它时,它会继续提供有关SSH问题的回溯,例如:

Traceback (most recent call last):
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner

我想知道是否有可能不在屏幕上打印任何Traceback消息? 感谢

这是完整的错误:

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

2 个答案:

答案 0 :(得分:0)

你可以完成你的尝试,然后做一般性的捕获,然后不做任何事情。

git remote add new [cloned URL from above step]
git push new feature/XYZ

答案 1 :(得分:0)

可能最好的选择是使用tracebacklimit模块 对于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) 

对于Python 3.5(tracebacklimit),有些人报告说这应该设置为None才能工作,即:

sys.tracebacklimit = None

对于引发的多个异常,有些人报告不能保证工作(In Python, how do I print an error message without printing a traceback and close the program when a condition is not met?)。