0

libssh を使用してリモート コマンドをコンピューターに送信しています。このコマンドはリアルタイムなので、生成されたときにデータを取得しようとしています。基本的に、私はマウス イベントを 16 進ダンプしています。そのデータが入ってくると、それが必要になります。コマンドからリアルタイムで結果を返すにはどうすればよいですか?

#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;




}
}
4

2 に答える 2

0

問題はこの行にあります。

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

最後のパラメータは (0) Zero です。(1) one に変更すると、関数は Command の結果でバッファを埋めます。:D それだけです

于 2014-09-27T09:08:57.783 に答える