Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. (Utils::SshConnection) How to send a SSH remote command?
Forum Updated to NodeBB v4.3 + New Features

(Utils::SshConnection) How to send a SSH remote command?

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
3 Posts 2 Posters 9.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    telecaster
    wrote on 4 Apr 2014, 20:37 last edited by SGaist
    #1

    Dear Developers,

    Im quite stuck for a while, and any help to keep me going forward will be greatly appreciated!

    Actually I'm using The SshConnection integrated in the Qt Creator library. For unexperienced guys like me, It's quite difficult to work with the library as the documentation is very little.

    While creating a succesfull connection to the SSH Server, I'm not capable of sending a command, like e.g. "ls -la" to the server and getting the corresponding answer with the list of files/directories. how should it be implemented?

    Thanks to all in advance!

    Cheers,
    Marc

    Here is the code:

    @
    // com.h ==============================================================
    #ifndef COM_H
    #define COM_H

    #include <QObject>
    #include <QSharedPointer>
    #include "sshconnection.h"

    class Com : public QObject
    {
    Q_OBJECT
    public:
    explicit Com(QObject *parent = 0);
    void connecting(const QString &host, const QString &username, const QString &passwd);

    signals:

    public slots:

    private slots:

    void connected();
    void onConnectionError(QSsh::SshError);
    void onProcStarted();
    

    private:
    //QSsh::SshRemoteProcess::Ptr remoteProc;
    QSharedPointer QSsh::SshRemoteProcess *remoteProc;
    QSsh::SshConnection *connection;
    };

    #endif // COM_H
    // ===================================================================
    @

    @
    // com.cpp ============================================================
    #include "com.h"

    #include <QByteArray>

    Com::Com(QObject *parent) : QObject(parent), connection(0)
    {
    }

    void Com::connecting(const QString &host, const QString &username, const QString &passwd)
    {
    QSsh::SshConnectionParameters parameters;
    parameters.host = host;
    parameters.userName = username;
    parameters.password = passwd;
    parameters.timeout = 20;
    parameters.authenticationType = QSsh::SshConnectionParameters::AuthenticationTypePassword;
    parameters.port = 22;
    connection = new QSsh::SshConnection(parameters, this);

    connect(connection, SIGNAL(connected()), SLOT(connected()));
    connect(connection, SIGNAL(error(QSsh::SshError)), SLOT(onConnectionError(QSsh::SshError)));
    
    qDebug().nospace() << "COM: Connecting to host " << qPrintable(host) <<"...";
    
    connection->connectToHost();
    

    }

    void Com::onConnectionError(QSsh::SshError)
    {
    qDebug() << "Com: Connection error" << connection->errorString();
    }

    void Com::connected()
    {
    qDebug() << "COM: CONNECTED!";

    const QByteArray comman("ls");
    remoteProc = connection->createRemoteProcess(comman);--
    
    if(remoteProc){
        connect(remoteProc, SIGNAL(started()), SLOT(onProcStarted()));
        remoteProc->create();
    }
    

    }

    void Com::onProcStarted()
    {
    qDebug() << "COM: Channel Started";
    }
    // ===================================================================
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      telecaster
      wrote on 4 Apr 2014, 22:59 last edited by
      #2

      Ok...solved it by myself!!! It was time to go to sleep...hehehe
      @

      #ifndef COM_H
      #define COM_H

      #include <QObject>

      #include "sshremoteprocess.h"
      #include "sshconnection.h"

      class Com : public QObject
      {
      Q_OBJECT
      public:
      explicit Com(QObject *parent = 0);
      void connecting(const QString &host, const QString &username, const QString &passwd);

      signals:

      public slots:

      private slots:
      private slots:
      void connected();
      void onConnectionError(QSsh::SshError);
      void onChannelStarted();
      void readyReadStandardOutput();
      private:
      QSsh::SshRemoteProcess::Ptr remoteProc;
      QSsh::SshConnection *connection;
      };

      #endif // COM_H
      @

      @
      void Com::connected()
      {
      qDebug() << "COM: CONNECTED!";
      const QByteArray comman("ls");
      remoteProc = connection->createRemoteProcess(comman);

      if(remoteProc){
          connect(remoteProc.data(), SIGNAL(started()), SLOT(onChannelStarted()));
          connect(remoteProc.data(), SIGNAL(readyReadStandardOutput()), SLOT(readyReadStandardOutput()));
      
          remoteProc->start();
      }
      

      }

      void Com::onConnectionError(QSsh::SshError)
      {
      qDebug() << "Com: Connection error" << connection->errorString();
      }

      void Com::onChannelStarted(){
      qDebug() << "COM: Channel Started";
      }

      void Com::readyReadStandardOutput()
      {
      qDebug() << "OUTPUT:" << remoteProc->readAll();

      }@

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 5 Apr 2014, 21:05 last edited by SGaist
        #3

        Hi and welcome to devnet !

        Nice you found and thanks for sharing !

        Can you also mark the thread as solved using the "Topic Tool" button ? So other forum users may know a solution has been found :)

        [edit: updated solved marking request SGaist]

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        1/3

        4 Apr 2014, 20:37

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved