0

I have some piece of code like this

    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)

And when I run it, it keeps giving tracebacks about SSH problem such as this:

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

I would like to know if it is possible to not print at all on the screen any Traceback messages? Thanks

Here's the full error:

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
Bob Ebert
  • 1,342
  • 4
  • 22
  • 41

2 Answers2

0

You could just finish your try and just do a general catch and then do nothing with it.

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
soopanova
  • 11
  • 1
  • 2
0

Probably the best alternative would be to use the tracebacklimit module.
For Python 2.x you could do:

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) 

For Python 3.5 (tracebacklimit) some people have reported that this should be set to None to work, i.e.:

sys.tracebacklimit = None

For multiple exception raised some people reported is not guaranteed to work (In Python, how do I print an error message without printing a traceback and close the program when a condition is not met?).

Community
  • 1
  • 1
fedepad
  • 4,509
  • 1
  • 13
  • 27
  • In the link you provided: " When set to 0 or less, all traceback information is suppressed and only the exception type and value are printed.". This is the case, I still get an error, but I only get a resume. – Bob Ebert Jan 28 '17 at 23:14