# Buggy code
def _connect_proxy(
    self,
    proxy_host,
    proxy_port,
    proxy_pkey,
    user=None,
    password=None,
    num_retries=DEFAULT_RETRIES,
    retry_delay=RETRY_DELAY,
    allow_agent=True,
    timeout=None,
    forward_ssh_agent=False,
    keepalive_seconds=60,
    identity_auth=True,
):
    try:
        self._proxy_client = SSHClient(
            proxy_host,
            port=proxy_port,
            pkey=proxy_pkey,
            num_retries=num_retries,
            user=user,
            password=password,
            retry_delay=retry_delay,
            allow_agent=allow_agent,
            timeout=timeout,
            forward_ssh_agent=forward_ssh_agent,
            identity_auth=identity_auth,
            keepalive_seconds=keepalive_seconds,
            _auth_thread_pool=False,
        )
    except Exception as ex:
        msg = "Proxy authentication failed. Exception from tunnel client: %s"
        logger.error(msg, ex)
        raise ProxyError(msg, ex)
    if not FORWARDER.started.is_set():
        FORWARDER.start()
        FORWARDER.started.wait()
    FORWARDER.in_q.put((self._proxy_client, self.host, self.port))
    proxy_local_port = FORWARDER.out_q.get()
    return proxy_local_port


# Traceback (ssh2.exceptions.SocketRecvError)
Traceback (most recent call last):
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 194, in _init_session
THREAD_POOL.apply(self.session.handshake, (self.sock,))
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "ssh2\session.pyx", line 138, in ssh2.session.Session.handshake
File "ssh2\utils.pyx", line 214, in ssh2.utils.handle_error_codes
ssh2.exceptions.SocketRecvError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 194, in _init_session
THREAD_POOL.apply(self.session.handshake, (self.sock,))
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "ssh2\session.pyx", line 138, in ssh2.session.Session.handshake
File "ssh2\utils.pyx", line 214, in ssh2.utils.handle_error_codes
ssh2.exceptions.SocketRecvError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Albert\Desktop\mikrotik_test.py", line 63, in <module>
test = execute_command_and_return_output(my_command, host = host, password = "some_password", use_proxy = True)
File "C:\Users\Albert\Desktop\mikrotik_test.py", line 51, in execute_command_and_return_output
client = prepare_connection(host = host, password = password, use_proxy = use_proxy)
File "C:\Users\Albert\Desktop\mikrotik_test.py", line 28, in prepare_connection
client = SSHClient(dev_host, user = user, password = pwd, pkey = pkey, proxy_host=proxy_host, proxy_user=proxy_user, proxy_pkey=proxy_pkey)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 127, in __init__
super(SSHClient, self).__init__(
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 90, in __init__
self._init()
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 94, in _init
self._init_session()
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 200, in _init_session
return self._connect_init_session_retry(retries=retries+1)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 139, in _connect_init_session_retry
return self._init_session(retries=retries)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 200, in _init_session
return self._connect_init_session_retry(retries=retries+1)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\base\single.py", line 139, in _connect_init_session_retry
return self._init_session(retries=retries)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\pssh\clients\native\single.py", line 194, in _init_session
THREAD_POOL.apply(self.session.handshake, (self.sock,))
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\pool.py", line 161, in apply
return self.spawn(func, *args, **kwds).get()
File "src\\gevent\\event.py", line 305, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 335, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 323, in gevent._gevent_cevent.AsyncResult.get
File "src\\gevent\\event.py", line 303, in gevent._gevent_cevent.AsyncResult._raise_exception
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\_compat.py", line 65, in reraise
raise value.with_traceback(tb)
File "C:\Users\Albert\AppData\Local\Programs\Python\Python38\lib\site-packages\gevent\threadpool.py", line 142, in __run_task
thread_result.set(func(*args, **kwargs))
File "ssh2\session.pyx", line 138, in ssh2.session.Session.handshake
File "ssh2\utils.pyx", line 214, in ssh2.utils.handle_error_codes
ssh2.exceptions.SocketRecvError
[Finished in 28.4s with exit code 1]
[shell_cmd: python -u "C:\Users\Albert\Desktop\mikrotik_test.py"]
[dir: C:\Users\Albert\Desktop]
[path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\IDEMIA\AWP\Dlls;C:\Program Files\IDEMIA\AWP\Dlls;C:\Program Files (x86)\Google\Google Apps Migration\;C:\Program Files (x86)\Google\Google Apps Sync\;C:\Program Files\Git\cmd;C:\ProgramData\ComposerSetup\bin;C:\Users\Albert\AppData\Local\Programs\Python\Python38\Scripts\;C:\Users\Albert\AppData\Local\Programs\Python\Python38\;C:\Users\Albert\AppData\Local\Microsoft\WindowsApps;C:\Users\Albert\AppData\Local\Programs\Microsoft VS Code\bin;C:\php;C:\Users\Albert\AppData\Roaming\Composer\vendor\bin]

# Fixed code
def _connect_proxy(
    self,
    proxy_host,
    proxy_port,
    proxy_pkey,
    user=None,
    password=None,
    num_retries=DEFAULT_RETRIES,
    retry_delay=RETRY_DELAY,
    allow_agent=True,
    timeout=None,
    forward_ssh_agent=False,
    keepalive_seconds=60,
    identity_auth=True,
):
    assert isinstance(self.port, int)
    try:
        self._proxy_client = SSHClient(
            proxy_host,
            port=proxy_port,
            pkey=proxy_pkey,
            num_retries=num_retries,
            user=user,
            password=password,
            retry_delay=retry_delay,
            allow_agent=allow_agent,
            timeout=timeout,
            forward_ssh_agent=forward_ssh_agent,
            identity_auth=identity_auth,
            keepalive_seconds=keepalive_seconds,
            _auth_thread_pool=False,
        )
    except Exception as ex:
        msg = "Proxy authentication failed. Exception from tunnel client: %s"
        logger.error(msg, ex)
        raise ProxyError(msg, ex)
    if not FORWARDER.started.is_set():
        FORWARDER.start()
        FORWARDER.started.wait()
    FORWARDER.in_q.put((self._proxy_client, self.host, self.port))
    proxy_local_port = FORWARDER.out_q.get()
    return proxy_local_port