Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSHClient proxy default target port unset #248

Closed
algsupport opened this issue Dec 9, 2020 · 7 comments · Fixed by #250
Closed

SSHClient proxy default target port unset #248

algsupport opened this issue Dec 9, 2020 · 7 comments · Fixed by #250
Labels

Comments

@algsupport
Copy link

algsupport commented Dec 9, 2020

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
@algsupport
Copy link
Author

I am ok to work with ParallelSSHClient. I just thought you'd want to know.
Let me know if I can provide more info. Thank you.

@pkittenis pkittenis added invalid and removed invalid labels Dec 9, 2020
@pkittenis
Copy link
Member

pkittenis commented Dec 9, 2020

Thank you for the interest and report.

bastion_host is not a client parameter.

Can you please show complete code to reproduce the issue? Proxy configuration on the single client is tested, though there may be another issue.

@algsupport
Copy link
Author

@pkittenis Sorry, this was a typo while I was editing some sensitive data.
The parameter I use is actualy: proxy_host=bastion_host, proxy_user=bastion_user, proxy_pkey=bastion_pkey
I have also updated the code.

@algsupport
Copy link
Author

algsupport commented Dec 9, 2020

@pkittenis The above is the complete code.
I got this far before I got the issue. I started working with the sing sshclient because I need to catch errors on individual hosts when I noticed the issue. After that I switched to the parallelsshclient and it worked just by doing the minor changes shown above.

@pkittenis
Copy link
Member

Can you please enable debug logging and paste the output log?

from logging import DEBUG
from pssh.utils import logger, enable_logger
enable_logger(logger, DEBUG)

<your code here>

@algsupport
Copy link
Author

algsupport commented Dec 9, 2020

Hi I enabled the debug logging and found the issuse:

This is from ParallelSSHClient:
Client connected, forwarding 192.168.12.23:22 on remote host to 127.0.0.1:62081
This is from SSHClient:
Client connected, forwarding 192.168.12.23:None on remote host to 127.0.0.1:62124

For SSHClient the port is not defined by default and is set to None.
Once I set the port, SSHClient also worked.

@pkittenis pkittenis changed the title Possbile bug when using proxy/bastion with SSHClient. SSHClient proxy default target port unset Dec 9, 2020
@pkittenis
Copy link
Member

Thanks for the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants