ParallelSSH / ssh2-python

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

Exception occurred with the setenv() function #169

Closed dougpuob closed 2 years ago

dougpuob commented 2 years ago

Directions

Thanks the ssh2-python project! It gives me a great help :)

My SSH server is Windows 10, the client is an Ubuntu Linux. There is no argument with ssh2.channel.execute() function to specific a working directory, so I'd like to call ssh2.channel.setenv() before using execute() function, but I encountered a ssh2.exceptions.ChannelRequestDenied exception !

I tried to trace the source code with the ChannelRequestDenied keyword, then I found it was raised in libssh2/src/channel.c!channel_setenv() function. Seems it handles with "env" for shell in Unix-like environment only, and Windows should do with set in cmd.exe. (my guess)

libssh2/src/channel.c!channel_setenv()

static int channel_setenv(LIBSSH2_CHANNEL *channel,
                          const char *varname, unsigned int varname_len,
                          const char *value, unsigned int value_len) 
{
    ...

    *(s++) = SSH_MSG_CHANNEL_REQUEST;
    _libssh2_store_u32(&s, channel->remote.id);
    _libssh2_store_str(&s, "env", sizeof("env") - 1);
    *(s++) = 0x01;
    _libssh2_store_str(&s, varname, varname_len);
    _libssh2_store_str(&s, value, value_len);

    ...
    return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED,
                          "Unable to complete request for channel-setenv");
}

Bug reports

Code to reproduce

new_path = "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Users\gliuser\qemu-tasker;"

ret_setenv = ssh_chanl.setenv("PATH", new_path)

Traceback

Traceback (most recent call last):
  File "/home/dougpuob/workspace/dougpuob/qemu-tasker/qemu-tasker.git/src/module/sshclient.py", line 166, in execute
    ret_setenv = ssh_chanl.setenv("PATH", self.envvar_path)
  File "ssh2/channel.pyx", line 298, in ssh2.channel.Channel.setenv
  File "ssh2/utils.pyx", line 174, in ssh2.utils.handle_error_codes
ssh2.exceptions.ChannelRequestDenied

Expected behaviour: [What was expected to happen.]

The setenv() function work without exceptions, then the PATH environment variable will be changed to new one.

Actual behaviour: [What actually happened.]

I encountered a ssh2.exceptions.ChannelRequestDenied exception.

Additional info: [Include version of libssh2 and any other relevant information.]

I known I can combine my command with the cd command, but sometimes I got unexpected value with the get_exit_status() function.

pkittenis commented 2 years ago

Hi there,

Thanks for the interest and report.

ChannelRequestDenied is returned by the server when the server does not support the channel request.

This is server dependent and is not an issue with the library.