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



  • 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";
    }
    // ===================================================================
    @



  • 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();

    }@


  • Lifetime Qt Champion

    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]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.