mardi 21 avril 2015

sending command to remote PC using LIBSSH

I using libssh to write command to remote PC.So its like (MY PC->Linux MAchine->Remote PC).iam able to to do port forwarding but when i try to exeute command let say ls -l its just wait .

For refrence here is my code :

void StartSession()
{
        int rc;
          m_host = "10.99.93.26";
          m_user = "embadmin";
          m_password = "#####";
          m_port = 22;
          m_loggedIn = false;
          m_session  = NULL;
          m_workingDirectory = ".";
          qRegisterMetaType<ConnectorWidget::SshError>("ConnectorWidget::SshError");
          int verbosity = SSH_LOG_FUNCTIONS;
          m_session = ssh_new();
          qDebug()<<"m_host.data()"<<m_host.toLatin1();
          // set the pertinant ssh session options
          ssh_options_set(m_session, SSH_OPTIONS_HOST, m_host.toLatin1());
          ssh_options_set(m_session, SSH_OPTIONS_USER, m_user.toLatin1());
          ssh_options_set(m_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
          ssh_options_set(m_session, SSH_OPTIONS_PORT, &m_port);

         int connectionResponse = ssh_connect(m_session);

         qDebug()<<"connectionResponse"<<connectionResponse;
         // if connection is Successful keep track of connection info.
         if (connectionResponse == SSH_OK)
         {
             qDebug()<<"Connectede";

         }
         else
         {
             qDebug()<<"Not Connected";

         }


         rc = ssh_userauth_password(m_session,NULL,m_password.toUtf8().constData());
         if ( rc == SSH_AUTH_SUCCESS)
         {
             qDebug()<<"Authenticated correctly";

         }

         ssh_channel channel;

         channel = ssh_channel_new(m_session);
         if (channel == NULL) qDebug()<<"channel NULL";
         rc = ssh_channel_open_session(channel);
         if (rc != SSH_OK)
         {
           ssh_channel_free(channel);
         }
    direct_forwarding(m_session);
    }

    void ConnectorWidget::direct_forwarding(ssh_session session)
    {

        char buffer[1024*1024];

         int total=0;
         int lastshown=4096;

        ssh_channel forwarding_channel;
         forwarding_channel = ssh_channel_new(session);;
        int rc = ssh_channel_open_forward(forwarding_channel,
                                      "10.99.92.80", 2222,
                                      "10.99.91.27", 80);

        if (rc != SSH_OK)
        {
            ssh_channel_free(forwarding_channel);
            qDebug()<<"rc after port forwarding";
        }
        rc = ssh_channel_request_exec(forwarding_channel, "cat > /dev/null");
         if (rc < 0) {
           ssh_channel_close(forwarding_channel);
           ssh_disconnect(session);

         }


         while ((rc = ssh_channel_write(forwarding_channel, buffer, sizeof(buffer))) > 0) {
           total += rc;
           if(total/2 >= lastshown){
             printf("written %llx\n", (long long unsigned int) total);
             lastshown=total;
           }
           if(total > LIMIT)
             break;
         }

         if (rc < 0) {
           printf("error : %s\n",ssh_get_error(session));
           ssh_channel_close(forwarding_channel);
           ssh_disconnect(session);

         }

         ssh_channel_send_eof(forwarding_channel);
         ssh_channel_close(forwarding_channel);

         ssh_disconnect(session);





    }

Can any body tell me whats wrong with this..

Thanks

Aucun commentaire:

Enregistrer un commentaire