简体   繁体   中英

libssh not return command results

I am using libssh to send a remote command to a computer. This command is real time so I am trying to get databack as it is generated. Basically I am hexdumping a mouse event and I want that data as it comes in. How can I make this return realtime results from my command?

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




}
}

If you want to get asynchronous real-time responses from remote file being changed, you'd better try some special async I/O API, like libevent . You will have to write your own client and server, but it's quite simple. Are you sure you need an encrypted connection? If you are, openSSL is supported by libevent too.

The Problem is in this line my friend

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

The last parameters is (0) Zero . If you Change it to (1) one , function will fill the buffer with the result of your Command . :D That's it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM