我正在使用libssh在远程服务器上执行命令。 这里是我的代码(返回代码这里不检查简化,但是所有的人都OK):stderr与libssh在pty模式下
#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;
}
输出是axpected:
stdout: foo
stderr: command not found: whoam
现在,如果我添加调用ssh_channel_request_pty
只是ssh_channel_open_session
后:
...
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
我得到以下错误的:
须藤:对不起,你必须有一个tty运行sudo的
感谢您的解释。不幸的是,我不允许ssh作为根用户或修改这些服务器上的sudoers配置......我想我必须找到解决方法! – julienc