Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SSH Connection with Remote Linux devices
Forum Updated to NodeBB v4.3 + New Features

SSH Connection with Remote Linux devices

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 4 Posters 6.6k Views
  • 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.
  • V Vijaykarthikeyan

    I have a shell script which should be run in rasberry pi which is a linux OS based system. I have tested in command prompt to ensure the file should be openable.

    Microsoft Windows [Version 10.0.19045.3803]
    (c) Microsoft Corporation. All rights reserved.
    
    C:\Users\hp>ssh votpl@192.168.56.1
    username@host
     password:
    Linux pi 6.1.21+ #1642 Mon Apr  3 17:19:14 BST 2023 armv6l
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    Last login: Sat Jan  6 21:49:32 2024 from 192.168.56.100
    
    Wi-Fi is currently blocked by rfkill.
    Use raspi-config to set the country before use.
    
    username@pi:~ $ cd Desktop
    username@pi:~/Desktop $ ./ROV.sh
    python: no process found
    No Arduino
    connection reset
    No Arduino
    

    This is the command prompt response for the ssh connection.Now , the file is opening via command prompt.Now,I want to automate this process..So,that on running a script in Qt..I should connect via ssh automatically at the backend.Screenshot 2024-01-19 145826.png

    How to script this process.I have used QProcess but it throws error like this:
    SSH Error: "Process failed to start: The system cannot find the file specified.

    // SshHandler.cpp
    #include "SshHandler.h"
    #include "qdebug.h"
    
    SshHandler::SshHandler(QObject *parent)
        : QObject(parent)
        , sshProcess(new QProcess(this))
    {
    }
    
    void SshHandler::connectToRemoteDevice(const QString &host,
                                           const QString &username,
                                           const QString &password)
    {
        // Assemble the SSH command
        QStringList sshArgs;
    
        sshArgs << "-T"
                << "ssh" << username + "@" + host << password << "cd Desktop && ./ROV.sh";
    
        // Start the SSH process
        sshProcess->start("ssh", sshArgs);
    
        qDebug() << "SSH Error: " << sshProcess->errorString();
    
    }
    
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #2

    @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

    SSH Error: "Process failed to start: The system cannot find the file specified

    Well, make sure the ssh executable can be found. For example you can use absolute path to ssh.exe

    Also, you should not call errorString right after starting the process because start() is asynchronous. Better connect a slot to the error signal.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    Christian EhrlicherC V 2 Replies Last reply
    1
    • jsulmJ jsulm

      @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

      SSH Error: "Process failed to start: The system cannot find the file specified

      Well, make sure the ssh executable can be found. For example you can use absolute path to ssh.exe

      Also, you should not call errorString right after starting the process because start() is asynchronous. Better connect a slot to the error signal.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      And why is sh needed here at all? Start ssh directly.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        And why is sh needed here at all? Start ssh directly.

        V Offline
        V Offline
        Vijaykarthikeyan
        wrote on last edited by
        #4

        @Christian-Ehrlicher ok edited..But,even though im running it shows, SSH Error: "Unknown error".

        I don't know the correct command line arguments..can someone guide me please

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

          SSH Error: "Process failed to start: The system cannot find the file specified

          Well, make sure the ssh executable can be found. For example you can use absolute path to ssh.exe

          Also, you should not call errorString right after starting the process because start() is asynchronous. Better connect a slot to the error signal.

          V Offline
          V Offline
          Vijaykarthikeyan
          wrote on last edited by
          #5

          @jsulm like this?

              connect(sshProcess,
                      &QProcess::readyReadStandardOutput,
                      this,
                      &SshHandler::onSshReadyReadStandardOutput);
              connect(sshProcess,
                      &QProcess::readyReadStandardError,
                      this,
                      &SshHandler::onSshReadyReadStandardError);
          
          Christian EhrlicherC jsulmJ 2 Replies Last reply
          0
          • V Vijaykarthikeyan

            @jsulm like this?

                connect(sshProcess,
                        &QProcess::readyReadStandardOutput,
                        this,
                        &SshHandler::onSshReadyReadStandardOutput);
                connect(sshProcess,
                        &QProcess::readyReadStandardError,
                        this,
                        &SshHandler::onSshReadyReadStandardError);
            
            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            I doubt the password can be passed on the command line. Take a look on how to pass a password to ssh but for sure it's not as a simple argument.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            V 1 Reply Last reply
            0
            • V Vijaykarthikeyan

              @jsulm like this?

                  connect(sshProcess,
                          &QProcess::readyReadStandardOutput,
                          this,
                          &SshHandler::onSshReadyReadStandardOutput);
                  connect(sshProcess,
                          &QProcess::readyReadStandardError,
                          this,
                          &SshHandler::onSshReadyReadStandardError);
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

              like this?

              Did you see https://doc.qt.io/qt-6/qprocess.html#errorOccurred ?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                I doubt the password can be passed on the command line. Take a look on how to pass a password to ssh but for sure it's not as a simple argument.

                V Offline
                V Offline
                Vijaykarthikeyan
                wrote on last edited by
                #8

                @Christian-Ehrlicher yes..i thought that too.when i gone through examples,they have said sshpass for automating the authentication.But,I need to know that is that only problem..I dont know how the QProcess take the arguments..Im doubtful about the argument list. Although classes like TCP socket and sslsocket used to networking,but i dont know whether they could access passwords too.In Qt kits..There is an option for ssh feature but don't know that very well

                Christian EhrlicherC 1 Reply Last reply
                0
                • V Vijaykarthikeyan

                  @Christian-Ehrlicher yes..i thought that too.when i gone through examples,they have said sshpass for automating the authentication.But,I need to know that is that only problem..I dont know how the QProcess take the arguments..Im doubtful about the argument list. Although classes like TCP socket and sslsocket used to networking,but i dont know whether they could access passwords too.In Qt kits..There is an option for ssh feature but don't know that very well

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                  I dont know how the QProcess take the arguments..I

                  I don't see what this has to do with the password problem - you provide wrong arguments to ssh, ssh bails out with Unknown error - everything else would be surprising. Also ssh for sure does not simply take an argument cd Desktop && ./ROV.sh which is executed after the login.
                  You first have to inform you about the correct ssh parameters for your task. No Qt problem here.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  V 2 Replies Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                    I dont know how the QProcess take the arguments..I

                    I don't see what this has to do with the password problem - you provide wrong arguments to ssh, ssh bails out with Unknown error - everything else would be surprising. Also ssh for sure does not simply take an argument cd Desktop && ./ROV.sh which is executed after the login.
                    You first have to inform you about the correct ssh parameters for your task. No Qt problem here.

                    V Offline
                    V Offline
                    Vijaykarthikeyan
                    wrote on last edited by
                    #10

                    @Christian-Ehrlicher Yes of course..that's what i have said that i have doubtful about the arguments..i dont know how to give the arguments likewise in command line. There is no Qt documentation for this.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                      I dont know how the QProcess take the arguments..I

                      I don't see what this has to do with the password problem - you provide wrong arguments to ssh, ssh bails out with Unknown error - everything else would be surprising. Also ssh for sure does not simply take an argument cd Desktop && ./ROV.sh which is executed after the login.
                      You first have to inform you about the correct ssh parameters for your task. No Qt problem here.

                      V Offline
                      V Offline
                      Vijaykarthikeyan
                      wrote on last edited by
                      #11

                      @Christian-Ehrlicher So even before im trying to read the file in the remote directory,i want to ensure that whether it is connected or not by just giving hostname and username but im receiving this:

                      SSH Error: "ssh: connect to host port 22: Connection refused\r\n"

                      jsulmJ 1 Reply Last reply
                      0
                      • V Vijaykarthikeyan

                        @Christian-Ehrlicher So even before im trying to read the file in the remote directory,i want to ensure that whether it is connected or not by just giving hostname and username but im receiving this:

                        SSH Error: "ssh: connect to host port 22: Connection refused\r\n"

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @Vijaykarthikeyan You should not use passwords but identity files, then there is no need to provide password.

                        "ssh: connect to host port 22: Connection refused" - this means that there is no SSH server listening on port 22 on that machine, again nothing Qt specific.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        V 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @Vijaykarthikeyan You should not use passwords but identity files, then there is no need to provide password.

                          "ssh: connect to host port 22: Connection refused" - this means that there is no SSH server listening on port 22 on that machine, again nothing Qt specific.

                          V Offline
                          V Offline
                          Vijaykarthikeyan
                          wrote on last edited by
                          #13

                          @jsulm are the key files to be located on both client and server sides? or from development side?in Qt tools,there is an option for ssh connection
                          Screenshot 2024-01-19 164812.png

                          can i refer this.
                          https://doc.qt.io/qtcreator/creator-developing-generic-linux.html

                          (or)
                          can we use sshpass which is helpful for scripting?

                          jsulmJ 1 Reply Last reply
                          0
                          • V Vijaykarthikeyan

                            @jsulm are the key files to be located on both client and server sides? or from development side?in Qt tools,there is an option for ssh connection
                            Screenshot 2024-01-19 164812.png

                            can i refer this.
                            https://doc.qt.io/qtcreator/creator-developing-generic-linux.html

                            (or)
                            can we use sshpass which is helpful for scripting?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                            can i refer this.

                            What you're doing is not related to QtCreator.
                            Not QtCreator is accessing the device via SSH but your application using QProcess and ssh executable. You need to generate a key file and upload it to your device, see for example https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            V 2 Replies Last reply
                            2
                            • jsulmJ jsulm

                              @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                              can i refer this.

                              What you're doing is not related to QtCreator.
                              Not QtCreator is accessing the device via SSH but your application using QProcess and ssh executable. You need to generate a key file and upload it to your device, see for example https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

                              V Offline
                              V Offline
                              Vijaykarthikeyan
                              wrote on last edited by
                              #15

                              @jsulm ok..thank you ..working..I have successfully run the remote linux device via command line terminal

                              1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                                can i refer this.

                                What you're doing is not related to QtCreator.
                                Not QtCreator is accessing the device via SSH but your application using QProcess and ssh executable. You need to generate a key file and upload it to your device, see for example https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

                                V Offline
                                V Offline
                                Vijaykarthikeyan
                                wrote on last edited by
                                #16

                                @jsulm Now,I have the key and i tested in command terminal and it works well. Now,i don't know the command line arguments for linux to be used in QProcess. when goes through the documentation,it says the start function takes command line arguments but not mentioned the correct format. Can someone guide me how to use the command line arguments for linux to be run correctly in QProcess.It should open the file in desktop directory after connecting with the linux host

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • V Vijaykarthikeyan

                                  @jsulm Now,I have the key and i tested in command terminal and it works well. Now,i don't know the command line arguments for linux to be used in QProcess. when goes through the documentation,it says the start function takes command line arguments but not mentioned the correct format. Can someone guide me how to use the command line arguments for linux to be run correctly in QProcess.It should open the file in desktop directory after connecting with the linux host

                                  Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                                  not mentioned the correct format

                                  What does this mean? You don't need to re-format the arguments in any way. Just pass them to QProcess::setArguments(). The detailed description also has an example on how to pass arguments.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  V 1 Reply Last reply
                                  1
                                  • Christian EhrlicherC Christian Ehrlicher

                                    @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                                    not mentioned the correct format

                                    What does this mean? You don't need to re-format the arguments in any way. Just pass them to QProcess::setArguments(). The detailed description also has an example on how to pass arguments.

                                    V Offline
                                    V Offline
                                    Vijaykarthikeyan
                                    wrote on last edited by Vijaykarthikeyan
                                    #18

                                    @Christian-Ehrlicher ok..when i included this after starting the process:

                                    qDebug() << sshProcess->state();
                                    
                                            qDebug() << "SSH Output:" << sshProcess->readAllStandardOutput();
                                            qDebug() << "SSH Error Output:" << sshProcess->readAllStandardError();
                                    

                                    it gives like this:

                                    QProcess::Running
                                    SSH Output: ""
                                    SSH Error Output: ""

                                    why?it is not printing..readAllStandardOutput and readAllStandardError are QBytArrays..

                                    Can I consider this as my program executed correctly?

                                    Because on terminal, when successfully connected and executing the shell scirpt..it runs like this:

                                    python: no process found
                                    No Arduino
                                    connection reset
                                    No Arduino
                                    connection reset
                                    No Arduino

                                    In my Qt,it should print like this

                                    V JonBJ 2 Replies Last reply
                                    0
                                    • V Vijaykarthikeyan

                                      @Christian-Ehrlicher ok..when i included this after starting the process:

                                      qDebug() << sshProcess->state();
                                      
                                              qDebug() << "SSH Output:" << sshProcess->readAllStandardOutput();
                                              qDebug() << "SSH Error Output:" << sshProcess->readAllStandardError();
                                      

                                      it gives like this:

                                      QProcess::Running
                                      SSH Output: ""
                                      SSH Error Output: ""

                                      why?it is not printing..readAllStandardOutput and readAllStandardError are QBytArrays..

                                      Can I consider this as my program executed correctly?

                                      Because on terminal, when successfully connected and executing the shell scirpt..it runs like this:

                                      python: no process found
                                      No Arduino
                                      connection reset
                                      No Arduino
                                      connection reset
                                      No Arduino

                                      In my Qt,it should print like this

                                      V Offline
                                      V Offline
                                      Vijaykarthikeyan
                                      wrote on last edited by
                                      #19

                                      @Vijaykarthikeyan Now,it ic successfully connected,

                                      SSH Output: "Linux pi 6.1.21+ #1642 Mon Apr 3 17:19:14 BST 2023 armv6l\n\nThe programs included with the Debian GNU/Linux system are free software;\nthe exact distribution terms for each program are described in the\nindividual files in /usr/share/doc/*/copyright.\n\nDebian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\npermitted by applicable law.\n\nWi-Fi is currently blocked by rfkill.\nUse raspi-config to set the country before use.\n\n"

                                      But,when i give the command to navigate to the file directory it says too many arguments

                                      1 Reply Last reply
                                      0
                                      • V Vijaykarthikeyan

                                        @Christian-Ehrlicher ok..when i included this after starting the process:

                                        qDebug() << sshProcess->state();
                                        
                                                qDebug() << "SSH Output:" << sshProcess->readAllStandardOutput();
                                                qDebug() << "SSH Error Output:" << sshProcess->readAllStandardError();
                                        

                                        it gives like this:

                                        QProcess::Running
                                        SSH Output: ""
                                        SSH Error Output: ""

                                        why?it is not printing..readAllStandardOutput and readAllStandardError are QBytArrays..

                                        Can I consider this as my program executed correctly?

                                        Because on terminal, when successfully connected and executing the shell scirpt..it runs like this:

                                        python: no process found
                                        No Arduino
                                        connection reset
                                        No Arduino
                                        connection reset
                                        No Arduino

                                        In my Qt,it should print like this

                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by
                                        #20

                                        @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                                        why?it is not printing..readAllStandardOutput and readAllStandardError

                                        As I presume you have discovered, you will not receive any output from a subprocess if you ask for it immediately after starting it --- no output has yet been produced. You either need to connect() to readyRead.,.,.(), call waitForReadyRead() (yuck!) or wait until process finished.

                                        But,when i give the command to navigate to the file directory it says too many arguments

                                        For people to help you, don't you think we need to see what "command" you are issuing, how you are issuing it, and just what the error message reads? Or do you think we can tell you what is wrong with your "command to navigate to the file directory" without knowing what you have done?

                                        V 2 Replies Last reply
                                        1
                                        • JonBJ JonB

                                          @Vijaykarthikeyan said in SSH Connection with Remote Linux devices:

                                          why?it is not printing..readAllStandardOutput and readAllStandardError

                                          As I presume you have discovered, you will not receive any output from a subprocess if you ask for it immediately after starting it --- no output has yet been produced. You either need to connect() to readyRead.,.,.(), call waitForReadyRead() (yuck!) or wait until process finished.

                                          But,when i give the command to navigate to the file directory it says too many arguments

                                          For people to help you, don't you think we need to see what "command" you are issuing, how you are issuing it, and just what the error message reads? Or do you think we can tell you what is wrong with your "command to navigate to the file directory" without knowing what you have done?

                                          V Offline
                                          V Offline
                                          Vijaykarthikeyan
                                          wrote on last edited by
                                          #21

                                          @JonB go through my preivous replies,i have already quoted this signal slot mechanism..please read that too..and @jsulm already corrected me for using corrrect signal and slot..

                                          1 Reply Last reply
                                          0

                                          • Login

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