2012-01-03 5 views
0

libssh를 사용하여 컴퓨터에 원격 명령을 보내고 있습니다. 이 명령은 실시간으로 생성되므로 databack을 생성하려고합니다. 기본적으로 나는 마우스 이벤트를 덤핑하고 있기 때문에 그 데이터가 들어 오길 원합니다. 내 명령에서이 결과를 실시간으로 반환하려면 어떻게해야합니까? 당신이 변경되는 원격 파일에서 비동기 실시간 반응을 얻고 싶다면libssh가 명령 결과를 반환하지 않습니다.

#include <libssh/libssh.h> 

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



/* 
* 1) Set ssh options 
* 2) Connect 
* 3) Authenticate 
* 4) Set channels 
* 5) Execute command 
* */ 


int main() 
{ 

    //Initilization 
    ssh_session session; 
    int verbosity = SSH_LOG_PROTOCOL; 
    int port = 22; 

    char* password ="root"; 
    int rc; 


    session = ssh_new(); 
    if (session == NULL) 
     return(-1); 

    //Set options for SSH connection 
    ssh_options_set(session,SSH_OPTIONS_HOST,"90.12.34.44"); 
    ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY,&verbosity); 
    ssh_options_set(session,SSH_OPTIONS_PORT,&port); 

    ssh_options_set(session,SSH_OPTIONS_USER,"root"); 



    //Connect to server 

    rc = ssh_connect(session); 
    if (rc != SSH_OK) 
    { 
     fprintf(stderr,"Error connecting to host %s\n",ssh_get_error(session)); 
    ssh_free(session); 
    return(-1); 
    } 



    rc = ssh_userauth_password(session,NULL,password); 
    if (rc == SSH_AUTH_SUCCESS) 
    { 
     printf("Authenticated correctly"); 

    } 


    ssh_channel channel; 
    channel = ssh_channel_new(session); 
    if(channel == NULL) return SSH_ERROR; 

    rc = ssh_channel_open_session(channel); 
    if (rc != SSH_OK) 
    { 
     ssh_channel_free(channel); 
     return rc; 
    } 


    rc = ssh_channel_request_exec(channel,"hd /dev/input/event0"); 
    if (rc != SSH_OK) 
    { 
     ssh_channel_close(channel); 
     ssh_channel_free(channel); 
     return rc; 
    } 



    char buffer[30]; 
    unsigned int nbytes; 

    nbytes = ssh_channel_read(channel,buffer,sizeof(buffer),0); 
    while(nbytes > 0) 
    { 
     if(fwrite(buffer,1,nbytes,stdout)); 
     { 
      ssh_channel_close(channel); 
     ssh_channel_free(channel); 
     return SSH_ERROR; 

     } 

     nbytes = ssh_channel_read(channel,buffer,sizeof(buffer),0); 


    if (nbytes < 0) 
    { 

     ssh_channel_close(channel); 
    ssh_channel_free(channel); 
    return SSH_ERROR; 
    } 

    return 0; 




} 
} 
+0

그래서 당신의 질문은 무엇입니까?! –

+2

'fwrite'는 쓰여진 "items"의 수를 반환하므로,'stdout'에 성공적으로 쓰는 것은 여러분의 코드에서 에러로 처리 될 것입니다. –

답변

0

, 당신은 더 나은 libevent 같은 특별한 비동기 I/O API를 시도 할 것입니다. 자신의 클라이언트와 서버를 작성해야하지만 매우 간단합니다. 암호화 된 연결이 필요합니까? 현재 openSSL은 libevent도 지원합니다.

0

문제는

nbytes = ssh_channel_read(channel,buffer,sizeof(buffer),0); 

마지막 매개 변수 (0) 제로 내 친구가이 라인에있다. 이것을 (1)로 변경하면 함수가 버퍼의 결과를 명령으로 채 웁니다. : D 그게 다야

관련 문제