How to detect when ssh connection (over a QProcess) has finished?

后端 未结 2 1483
清酒与你
清酒与你 2021-01-23 10:41

I am running an ssh tunnel from an application using a QProcess:

QProcess* process = new QProcess();
process->start(\"ssh\", QStringList()<<         


        
相关标签:
2条回答
  • 2021-01-23 11:24

    I had the same problem, but in my case ssh do not output anything - so I couldn't just wait for output. I'm also using ssh to setupt tunnel, so I used QTcpSocket:

    program = "ssh";
    arguments << m_host << "-N" << "-L" << QString("3306:%1:3306").arg(m_host);
    connect(tunnelProcess, &QProcess::started, this, &Database::waitForTunnel);
    tunnelProcess->start(program, arguments);
    

    waitForTunnel() slot:

    QTcpSocket sock;
    sock.connectToHost("127.0.0.1", 3306);
    if(sock.waitForConnected(100000))
    {
         sock.disconnectFromHost();
         openDatabaseConnection();
    }
    else
        qDebug() << "timeout";
    

    I hope this will help future people finding this question ;)

    0 讨论(0)
  • 2021-01-23 11:30

    So, the only question left is, can anyone help me without any concerns in this approach?

    This is not a stable and robust solution, unfortunately. It is similarly a broken concept to handling git outputs rather than using an actual library. The main problem is that these softwares do not have any guarantee for output compatibility, rightfully.

    Just imagine that what happens if they have an unclear text, a typo, et all, unnoticed. They inherently need to fix the output respectively, and all the applications relying on the output would abruptly break.

    This is also the reason behind working on dedicated libraries giving access to the functionality for reuse rather than working with the user facing output directly. In case of git, this means the libgit2 library, for instance.

    Qt does not have an ssh mechanism in place by default like you can have such libraries in python, e.g. paramiko.

    I would suggest to establish a way in your code by using libssh or libssh2 as you also noted yourself in the comment. I can understand the inconvenience that is not a truly Qt'ish way as of now, but at this point Qt cannot provide anything more robust without third-party.

    That being said, it would be nice to see a similar add-on library in the Qt Project for the future, but this may not be happen any soon. If you write your software with proper design in mind, you will be able to switch to such a library withour major issues once someone stands up to maintain such an additional library to Qt or elsewhere.

    0 讨论(0)
提交回复
热议问题