在 pty 模式下使用 libssh 的 stderr

stderr with libssh in pty mode

我正在使用 libssh 在远程服务器上执行命令。 这是我的代码(return 代码在这里为了简化没有检查,但都可以):

#include <stdio.h>
#include <stdlib.h>
#include <libssh/libssh.h>

int main() {

    /* opening session and channel */
    ssh_session session = ssh_new();
    ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
    ssh_options_set(session, SSH_OPTIONS_PORT_STR, "22");
    ssh_options_set(session, SSH_OPTIONS_USER, "julien");
    ssh_connect(session);
    ssh_userauth_autopubkey(session, NULL);
    ssh_channel channel = ssh_channel_new(session);
    ssh_channel_open_session(channel);

    /* command execution */
    ssh_channel_request_exec(channel, "echo 'foo' && whoam");

    char *buffer_stdin = calloc(1024, sizeof(char));
    ssh_channel_read(channel, buffer_stdin, 1024, 0);
    printf("stdout: %s\n", buffer_stdin);
    free(buffer_stdin);

    char *buffer_stderr = calloc(1024, sizeof(char));
    ssh_channel_read(channel, buffer_stderr, 1024, 1);
    printf("stderr: %s", buffer_stderr);
    free(buffer_stderr);

    ssh_channel_free(channel);
    ssh_free(session);
    return EXIT_SUCCESS;
}

输出符合预期:

stdout: foo
stderr: command not found: whoam

现在,如果我在 ssh_channel_open_session 之后添加对 ssh_channel_request_pty 的调用:

    ...
    ssh_channel channel = ssh_channel_new(session);
    ssh_channel_open_session(channel);
    ssh_channel_request_pty(channel);
    ssh_channel_request_exec(channel, "echo 'foo' && whoam");
    ...

不再有 stderr 输出:

stdout: foo
stderr:

如果我通过以下方式更改命令:

ssh_channel_request_exec(channel, "whoam");

现在在标准输出上读取错误输出!

stdout: command not found: whoam
stderr:

我在 ssh_channel_request_pty 中遗漏了什么?

有关信息,我正在使用它,因为在某些服务器上,当 运行 使用 sudo:

的命令时,我会收到以下错误

sudo: sorry, you must have a tty to run sudo

从概念上讲,一个 pty 代表一个终端(或者,在更低的级别上,一个串行端口)——每个方向上只有一个通道。默认情况下,pty 中进程 运行ning 的标准输出和标准错误都会转到相同的位置。

(考虑当您在终端中 运行 一个命令时会发生什么,例如——如果您没有重定向任何东西,stdout 和 stderr 都会出现在屏幕上,并且没有办法告诉他们分开。)

一般来说,如果您需要以 root 身份远程自动执行 运行ning 命令,您应该以 root 身份通过 SSH 登录,或者使用 NOPASSWD 选项授予用户 sudo 访问权限。不要自动输入密码。