-
Notifications
You must be signed in to change notification settings - Fork 154
Closed
Labels
Description
Hello,
Thank you for you great project. I think I found a bug with single SSHClient.
Please consider the following code:
from pssh.clients import SSHClient
from pssh.clients import ParallelSSHClient
bastion = 'bastion ip'
username = 'username'
private_key = 'my_key'
my_command = 'my command'
test_hosts = ["host23","host27","host57","host69"]
def prepare_connection(host = None, password = None, use_bastion = False):
pkey = private_key
user = username
pwd = None
dev_host = bastion
bastion_host = None
bastion_user = None
bastion_pkey = None
if use_bastion:
pkey = None
user = "host_username"
pwd = password
dev_host = host
bastion_host = bastion
bastion_user = username
bastion_pkey = private_key
client = ParallelSSHClient([dev_host], user = user, password = pwd, pkey = pkey, proxy_host=bastion_host, proxy_user=bastion_user, proxy_pkey=bastion_pkey)
return client
def execute_command_and_return_output(command, use_filter=True, host = None, password = None, use_bastion=False):
client = prepare_connection(host = host, password = password, use_bastion = use_bastion)
host_out = client.run_command(command)
return "".join(list(host_out[0].stdout))
for host in test_hosts:
test = execute_command_and_return_output(my_command, host = host, password = "some_password", use_bastion = True)
print (test)
The above code works as expected. But, if I change:
client = ParallelSSHClient([dev_host], user = user, password = pwd, pkey = pkey, proxy_host=bastion_host, proxy_user=bastion_user, proxy_pkey=bastion_pkey)
with:
client = SSHClient(dev_host, user = user, password = pwd, pkey = pkey, proxy_host=bastion_host, proxy_user=bastion_user, proxy_pkey=bastion_pkey)
AND
return "".join(list(host_out[0].stdout))
with:
return "".join(list(host_out.stdout))
I get an expection as bellow:
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]
I am running python 3.8 on windows 10
pip freeze:
cffi==1.14.4
gevent==20.9.0
greenlet==0.4.17
parallel-ssh==2.3.1
pycparser==2.20
ssh-python==0.8.0.post1
ssh2-python==0.25.0
zope.event==4.5.0
zope.interface==5.2.0