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.3k 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

    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 Offline
            jsulmJ Offline
            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
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #22

                  ssh_askpass is not available on this linux distribution.

                  Kind Regards,
                  Sy

                  JonBJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    ssh_askpass is not available on this linux distribution.

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

                    @splatten
                    Yes, so you can't use it.

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

                      @JonB, I've tried everything you have suggested so far and not been able to make any progress due to either incorrect syntax or missing commands.

                      Kind Regards,
                      Sy

                      JonBJ 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        @JonB, I've tried everything you have suggested so far and not been able to make any progress due to either incorrect syntax or missing commands.

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

                        @splatten
                        I am suggesting your sftp client will not work to prompt for a password if you do not run it from a terminal. It will not work when you try redirection from a parent process like Qt.

                        The man page for my sftp under Ubuntu at least is pretty explicit about requiring either an "interactive" session for authentication, or must use ssh facilities for non-password authentication. Does your system have man sftp for you to read through?

                        1 Reply Last reply
                        1

                        • Login

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