Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt, implementing an SFTP client
Forum Updated to NodeBB v4.3 + New Features

Qt, implementing an SFTP client

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
25 Posts 3 Posters 7.4k 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.
  • SPlattenS SPlatten

    I'm working on a All-In-One Mobile Smart Card Terminal ACR890, this comes with a Qt SDK:

        Qt Creator 3.0.1
        Based on Qt 5.2.1 (GCC 4.8.2, 32 bit)
        Built on Apr 9 2014 at 09:14:10
    

    The development platform is:

        Ubuntu 14.04.6 LTS, Trusty Tahr
    

    I'm at the tail end of the project now, the only bit remaining is to implement an SFTP client so that software updates can be downloaded.

    I've create the GUI bits that allow the user to set-up the SFTP connection details, the only bit remaining is to connect to the SFTP server using the entered details.

    I've searched around and found links to:

    https://github.com/lvklabs/QSsh

    Which I downloaded, but the example will not build:

    cannot find -lbotan-2
    collect2: ld returned 1 exit status

    I then did a bit more searching and found a post on here, however since this is such an old project I thought it would be worth asking if there is anymore recent support for SFTP that I should take a look at for Qt?

    I think another possible problem is that the target device is ARM based, not i386. I installed both libssh and libssh2 and both of these only have i386 libraries.

    Also, I'd like to implement a multicolumn file view list to show the contents of the SFTP folder. Suggestions on this also welcome.

    jsulmJ Online
    jsulmJ Online
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @splatten said in Qt, implementing an SFTP client:

    I think another possible problem is that the target device is ARM based, not i386.

    This is not a problem.

    "I installed both libssh and libssh2 and both of these only have i386 libraries." - on your host machine I guess? You have to install them on your target device.

    "cannot find -lbotan-2" - you need to install that lib in your sysroot.

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

    1 Reply Last reply
    2
    • SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #3

      @jsulm, thank you. As the development laptop is i386, how can I install the arm version of libssh so I can build it on the laptop then deploy to the target device?

      Kind Regards,
      Sy

      jsulmJ 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @jsulm, thank you. As the development laptop is i386, how can I install the arm version of libssh so I can build it on the laptop then deploy to the target device?

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @splatten You have an SDK for your device, right? Maybe it provides the possibility to install additional packages?
        If not you can build these libs for your device using the SDK.

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

        1 Reply Last reply
        3
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #5

          @jsulm, I build on the laptop then deploy to the target, is the source available for libssh or libssh2 ?

          Kind Regards,
          Sy

          jsulmJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @jsulm, I build on the laptop then deploy to the target, is the source available for libssh or libssh2 ?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #6

            @splatten said in Qt, implementing an SFTP client:

            is the source available for libssh or libssh2

            yes, OpenSSH is open source

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

            1 Reply Last reply
            2
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #7

              @jsulm , thank you, I downloaded open ssh source and will take a look now.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #8

                Slightly different approach....using QProcess

                I plan to launch sftp and pass it the information for user name, address and port. Is there a way to grab the output from the console so I can look for the text "Password:" ?

                Kind Regards,
                Sy

                JonBJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  Slightly different approach....using QProcess

                  I plan to launch sftp and pass it the information for user name, address and port. Is there a way to grab the output from the console so I can look for the text "Password:" ?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @splatten
                  You are (presumably) using some third-party sftp.exe client? This is what I had to do in another (non-Qt) product. You should not have to parse output for things like host, port, username & password prompting. Look at the documentation for the sftp.exe you intend to use, there should/is likely to be command-line, or possibly take-from-file, for these?

                  1 Reply Last reply
                  0
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #10

                    @JonB , the sftp I'm using in present in the linux distribution on the device.

                    Kind Regards,
                    Sy

                    JonBJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @JonB , the sftp I'm using in present in the linux distribution on the device.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #11

                      @splatten
                      OK, well I don't know about that! What about man sftp (host, port, user at least on command line)? On, say, Ubuntu I see:

                       The final usage format allows for automated sessions using the -b option.
                      In such cases, it is necessary to configure non-interactive authentica‐
                      tion to obviate the need to enter a password at connection time (see
                      sshd(8) and ssh-keygen(1) for details).
                      

                      Anyway, you use QProcess methods like https://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput or (better) https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput signal to read from/write to the subprocess. You'll want to deal with all of stdin, stdout, & stderr, plus error signals, for proper control of your sub-process.

                      1 Reply Last reply
                      2
                      • SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by SPlatten
                        #12

                        Here is my sample code:

                        QProcess* pProc = new QProcess(this);
                        QString strHost = clsMainWnd::strGetHostName()
                               ,strPort = clsMainWnd::strGetPort()
                               ,strUsername = clsMainWnd::strGetUsername();
                        QStringList slstCmdArgs;
                        slstCmdArgs << (strUsername + QString("@") + strHost)
                                    << (QString("-P") + strPort);
                        pProc->start("sftp", slstCmdArgs);
                        while( pProc->waitForStarted() != true ) {
                        }
                        QByteArray bytaryIn = pProc->readAllStandardOutput();
                        qDebug() << bytaryIn;
                        

                        I've grabbed the content of slstCmdArgs and tried it in a terminal and it works, but in code I never see anything in "bytaryIn".

                        An example of what the code above does:

                            sftp User@HostName -P22
                        

                        What I am expecting to see is:

                            User@HostName's password:
                        

                        [Edit] I've now re-written this code to:

                        QString strHost = clsMainWnd::strGetHostName()
                               ,strPort = clsMainWnd::strGetPort()
                               ,strUsername = clsMainWnd::strGetUsername();
                        QStringList slstCmdArgs;
                        slstCmdArgs << (strUsername + QString("@") + strHost)
                                    << (QString("-P") + strPort);
                        mpSFTP = new QProcess(this);
                        connect(mpSFTP, SIGNAL(readyReadStandardOutput())
                                       ,this, SLOT(readSFTPoutput()));
                        mpSFTP->start("sftp", slstCmdArgs);
                        

                        Kind Regards,
                        Sy

                        jsulmJ JonBJ 2 Replies Last reply
                        0
                        • SPlattenS SPlatten

                          Here is my sample code:

                          QProcess* pProc = new QProcess(this);
                          QString strHost = clsMainWnd::strGetHostName()
                                 ,strPort = clsMainWnd::strGetPort()
                                 ,strUsername = clsMainWnd::strGetUsername();
                          QStringList slstCmdArgs;
                          slstCmdArgs << (strUsername + QString("@") + strHost)
                                      << (QString("-P") + strPort);
                          pProc->start("sftp", slstCmdArgs);
                          while( pProc->waitForStarted() != true ) {
                          }
                          QByteArray bytaryIn = pProc->readAllStandardOutput();
                          qDebug() << bytaryIn;
                          

                          I've grabbed the content of slstCmdArgs and tried it in a terminal and it works, but in code I never see anything in "bytaryIn".

                          An example of what the code above does:

                              sftp User@HostName -P22
                          

                          What I am expecting to see is:

                              User@HostName's password:
                          

                          [Edit] I've now re-written this code to:

                          QString strHost = clsMainWnd::strGetHostName()
                                 ,strPort = clsMainWnd::strGetPort()
                                 ,strUsername = clsMainWnd::strGetUsername();
                          QStringList slstCmdArgs;
                          slstCmdArgs << (strUsername + QString("@") + strHost)
                                      << (QString("-P") + strPort);
                          mpSFTP = new QProcess(this);
                          connect(mpSFTP, SIGNAL(readyReadStandardOutput())
                                         ,this, SLOT(readSFTPoutput()));
                          mpSFTP->start("sftp", slstCmdArgs);
                          
                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @splatten said in Qt, implementing an SFTP client:

                          QByteArray bytaryIn = pProc->readAllStandardOutput();

                          Don't do it like this - you don't know whether there is already anything to read. Use https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput signal instead.

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

                          1 Reply Last reply
                          4
                          • SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by
                            #14

                            @jsulm, having re-written the code, my slot never gets called, meaning the signal can't be emitted.

                            Kind Regards,
                            Sy

                            1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              Here is my sample code:

                              QProcess* pProc = new QProcess(this);
                              QString strHost = clsMainWnd::strGetHostName()
                                     ,strPort = clsMainWnd::strGetPort()
                                     ,strUsername = clsMainWnd::strGetUsername();
                              QStringList slstCmdArgs;
                              slstCmdArgs << (strUsername + QString("@") + strHost)
                                          << (QString("-P") + strPort);
                              pProc->start("sftp", slstCmdArgs);
                              while( pProc->waitForStarted() != true ) {
                              }
                              QByteArray bytaryIn = pProc->readAllStandardOutput();
                              qDebug() << bytaryIn;
                              

                              I've grabbed the content of slstCmdArgs and tried it in a terminal and it works, but in code I never see anything in "bytaryIn".

                              An example of what the code above does:

                                  sftp User@HostName -P22
                              

                              What I am expecting to see is:

                                  User@HostName's password:
                              

                              [Edit] I've now re-written this code to:

                              QString strHost = clsMainWnd::strGetHostName()
                                     ,strPort = clsMainWnd::strGetPort()
                                     ,strUsername = clsMainWnd::strGetUsername();
                              QStringList slstCmdArgs;
                              slstCmdArgs << (strUsername + QString("@") + strHost)
                                          << (QString("-P") + strPort);
                              mpSFTP = new QProcess(this);
                              connect(mpSFTP, SIGNAL(readyReadStandardOutput())
                                             ,this, SLOT(readSFTPoutput()));
                              mpSFTP->start("sftp", slstCmdArgs);
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #15

                              @splatten
                              Which is why I said to use https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput for better/proper behaviour....

                              There is a wrinkle you may have to face. It is possible that your command-line sftp executable will not prompt for password if it detects that the user is not in an interactive terminal (stdin). E.g. its code may quite possibly have

                              if (isatty(0))
                                  prompt_for_password();
                              else
                                 // do not prompt for password
                              

                              That will be problematic if it is the case. You could try in a terminal sftp < /dev/null or sftp < file: if that does not still prompt for password you have a (potential) problem....

                              1 Reply Last reply
                              1
                              • SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #16

                                @JonB , would this help:

                                    mpSFTP->setProcessChannelMode(QProcess::MergedChannels);
                                

                                Kind Regards,
                                Sy

                                JonBJ 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @JonB , would this help:

                                      mpSFTP->setProcessChannelMode(QProcess::MergedChannels);
                                  
                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by JonB
                                  #17

                                  @splatten
                                  Help what, in what way? If the sftp requires a terminal stdin for password prompt, then no, if, say, you have a purely interactive Qt application. You can try it. But the first thing to test is whether your sftp behaves differently if its stdin is not attached to a terminal?

                                  1 Reply Last reply
                                  1
                                  • SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #18

                                    Now the slot gets called and in my slot I have:

                                        qDebug() << "readAllStandardOutput:";
                                        qDebug() << mpSFTP->readAllStandardOutput();
                                        qDebug() << "readChannel:";
                                        qDebug() << mpSFTP->readChannel();
                                    

                                    In the output I see:

                                        readAllStandardOutput:
                                        "ssh_askpass: exec(/usr/local/libexec/ssh-askpass): No such file or directory
                                        "
                                        readChannel:
                                        0
                                    

                                    Kind Regards,
                                    Sy

                                    jsulmJ JonBJ 2 Replies Last reply
                                    0
                                    • SPlattenS SPlatten

                                      Now the slot gets called and in my slot I have:

                                          qDebug() << "readAllStandardOutput:";
                                          qDebug() << mpSFTP->readAllStandardOutput();
                                          qDebug() << "readChannel:";
                                          qDebug() << mpSFTP->readChannel();
                                      

                                      In the output I see:

                                          readAllStandardOutput:
                                          "ssh_askpass: exec(/usr/local/libexec/ssh-askpass): No such file or directory
                                          "
                                          readChannel:
                                          0
                                      
                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by jsulm
                                      #19

                                      @splatten What happens if you execute exact same command manually in a terminal?
                                      Also connect a slot to the error signal from QProcess and check whether you get any errors.

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

                                      1 Reply Last reply
                                      0
                                      • SPlattenS Offline
                                        SPlattenS Offline
                                        SPlatten
                                        wrote on last edited by SPlatten
                                        #20

                                        @jsulm , When I execute "sftp" with the command line arguments, it works without any issues.

                                        Kind Regards,
                                        Sy

                                        1 Reply Last reply
                                        0
                                        • SPlattenS SPlatten

                                          Now the slot gets called and in my slot I have:

                                              qDebug() << "readAllStandardOutput:";
                                              qDebug() << mpSFTP->readAllStandardOutput();
                                              qDebug() << "readChannel:";
                                              qDebug() << mpSFTP->readChannel();
                                          

                                          In the output I see:

                                              readAllStandardOutput:
                                              "ssh_askpass: exec(/usr/local/libexec/ssh-askpass): No such file or directory
                                              "
                                              readChannel:
                                              0
                                          
                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #21

                                          @splatten
                                          As I have said before, so last time now:

                                          ssh_askpass: exec(/usr/local/libexec/ssh-askpass): No such file or directory

                                          This is because your sftp is not willing/able to access stdin to get a password. Try what I said about seeing how your sftp behaves with input/output redirected when run from a terminal. I will not comment again, and leave it to you.

                                          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