私はlibsshを使用して、リモートサーバー上でコマンドを実行しています。 は、ここに私のコードです(リターンコードは簡略化のためにここでチェックされていないが、それらのすべてがOKです):ptyモードでlibsshを使用したstderr
stdout: foo
stderr: command not found: whoam
を今、私はへの呼び出しを追加した場合:
#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ようです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");
...
が、それ以上何も標準エラー出力はありません
stdout: foo
stderr:
そして、私はしてコマンドを変更した場合:
ssh_channel_request_exec(channel, "whoam");
今すぐエラー出力を標準出力に読まれます!
stdout: command not found: whoam
stderr:
ssh_channel_request_pty
で何かが不足していますか?私は理由sudo
でコマンドを実行しているとき、私は次のエラーを取得するいくつかのサーバー上でそれを使用していた情報については
、:
を実行するための端末を持っている必要がありますsudoを:申し訳ありませんが、あなたはsudoを
あなたの説明をありがとう。残念ながら、私はrootとしてsshすることはできませんし、それらのサーバ上のsudoersの設定を変更することも許されません...私は回避策を見つけなければならないでしょう! – julienc