2016-08-18 13 views
0

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 후 :

0 :

... 
    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를에게

를 실행하는 청각 장애를 가지고 있어야합니다

답변

2

개념적으로 pty는 터미널을 나타냅니다 (또는 심지어 더 낮은 레벨에서는 직렬 포트). 각 방향에는 하나의 채널 만 있습니다. pty에서 실행중인 프로세스의 표준 출력 및 표준 오류는 기본적으로 동일한 위치로 이동합니다.

(당신은 예를 들어, 터미널에서 명령을 실행할 때 어떤 일이 발생 고려 -. 당신은 아무것도 리디렉션하지 않은 경우 모두 표준 출력과 표준 에러가 화면에 나타납니다, 떨어져 그들에게 할 수있는 방법이 없습니다)

일반적으로 루트로 원격 명령 실행을 자동화해야하는 경우 루트로 SSH를 입력하거나 사용자에게 NOPASSWD 옵션으로 sudo 액세스 권한을 부여해야합니다. 암호 입력을 자동화하지 마십시오.

+0

설명해 주셔서 감사합니다. 불행히도 루트 권한으로 ssh를 사용하거나 해당 서버의 sudoers 구성을 수정하는 것은 허용되지 않습니다. 다음 해결 방법을 찾아야 할 것 같습니다! – julienc