ParallelSSH / ssh2-python

Python bindings for libssh2 C library.
https://parallel-ssh.org
GNU Lesser General Public License v2.1
228 stars 72 forks source link

BadUseError on second execute call #135

Closed GevatterGaul closed 3 years ago

GevatterGaul commented 3 years ago

Bug report

Description

I'm experiencing a bad use error when trying to execute more than one command on an open channel.

Steps to reproduce:

(testenv) [benjamin@mouse testenv]$ python
Python 3.8.6 (default, Sep 30 2020, 04:00:38) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ssh2.channel import Channel
>>> from ssh2.session import Session
>>> from socket import socket, AF_INET, SOCK_STREAM
>>> sock = socket(AF_INET, SOCK_STREAM)
>>> sock.connect(('localhost', 22))
>>> session = Session()
>>> session.handshake(sock)
0
>>> session.userauth_password('john', '123456')
0
>>> chan = session.open_session()
>>> chan.execute('echo "Hello World"')
0
>>> chan.execute('echo "Hello World"')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ssh2/channel.pyx", line 79, in ssh2.channel.Channel.execute
  File "ssh2/utils.pyx", line 198, in ssh2.utils.handle_error_codes
ssh2.exceptions.BadUseError

Expected behaviour:

I expected the second call to execute to complete as the first one did.

Actual behaviour:

I got a bad use error on the second execute.

Additional info:

System libssh2 has version 1.9.0-2 on Manjaro. ssh2-python has version 0.23.0 Python version 3.8.6 on 64bit Manjaro.

pkittenis commented 3 years ago

Hello,

Thank you for the interest.

ssh2.exceptions.BadUseError refers to bad API use. An execute call can only be done once per channel. Execute will need to be done after a new session.open_session().

Or use interactive shell with chan.shell().