Skip to content

Fail to open a shell #18

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

Closed
xavierhardy opened this issue Feb 11, 2018 · 3 comments
Closed

Fail to open a shell #18

xavierhardy opened this issue Feb 11, 2018 · 3 comments
Labels

Comments

@xavierhardy
Copy link

xavierhardy commented Feb 11, 2018

Bug report

Using libssh2 examples, I tried opening shell inside a channel in ssh2-python and it failed. I need to have a channel and a shell kept open for re-use. I cannot use exec because using exec to run a command closes the channel (which is expected).

Here is an example in the tests of libssh2:
https://github.com/libssh2/libssh2/blob/master/tests/ssh2.c#L152
libssh2_channel_shell is a macro:
https://github.com/libssh2/libssh2/blob/master/include/libssh2.h#L775

I get a LIBSSH2_ERROR_SOCKET_DISCONNECT error code (-13) when trying to open the shell.
https://github.com/libssh2/libssh2/blob/master/include/libssh2.h#L451

Steps to reproduce:

Run the SSH daemon locally and add an SSH public key to the authorized_hosts file of any user (here root)

Run the following Python script.

#!/usr/bin/env python
import socket

from ssh2.session import Session
from ssh2.utils import version

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 22))
session = Session()
session.handshake(sock)
session.userauth_publickey_fromfile(
    "root", "/root/.ssh/id_rsa.pub", "/root/.ssh/id_rsa", ""
)
channel = session.open_session()
print("pty")
err_code = channel.pty()
print(err_code)
print("shell")
err_code = channel.process_startup("shell", "")
print(err_code)

channel.close()
pty
0
shell
-13

Expected behaviour: 0 error code on shell request

Actual behaviour: error on shell request

Additional info:
libssh2-1:amd64 1.5.0-2ubuntu0.1
libssh2-1-dev:amd64 1.5.0-2ubuntu0.1
python-libssh2 1.0.0-1.2

@pkittenis pkittenis added the bug label Feb 12, 2018
pkittenis pushed a commit that referenced this issue Feb 13, 2018
…ypes with no message. Add integration tests for interactive shell request and process startup. Resolves #18.
pkittenis pushed a commit that referenced this issue Feb 13, 2018
…ypes with no message. Add integration tests for interactive shell request and process startup. Resolves #18.
@pkittenis
Copy link
Member

pkittenis commented Feb 13, 2018

Hi there,

Thanks for the interest and report.

Yes, this is an issue with the process_startup function. For shell requests, no message should be sent but the function does not allow that. An empty string as the message (as opposed to no message), is not accepted by the server which terminates the connection.

The next version will have a channel.shell() function which was missing - the libssh2 macro for shell process startup - as well as a revised process_startup which allows for no message.

From next version onwards:

channel = session.open_session()
channel.shell()
channel.write('my command\n')
size, data = channel.read()
<..>

Note that a pty is not explicitly required for interactive shells though it may be used.

@xavierhardy
Copy link
Author

Yes indeed, the macro in libssh2 uses a null pointer as message. My knowledge of Cython is limited, but I'll try to submit a pull request if I find the time. Thanks a lot!

@xavierhardy
Copy link
Author

That was fast, it's small fix but still. Thanks a lot, though I will open a new ticket. I would like to test your branch and the only way to find how to build it was to look at Travis. Keep up the good work!

Red-M pushed a commit to Red-M/ssh2-python that referenced this issue Jun 27, 2020
…ypes with no message. Add integration tests for interactive shell request and process startup. Resolves ParallelSSH#18.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants