Results 1 to 7 of 7

Thread: QProcess, plink, mysql

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QProcess, plink, mysql

    Hi, I am attempting to create a scenario where my program uses plink to create a ssh tunnel, and once it knows the tunnel is up, then log in to a mysql database. The pertinent piece of code that does this is as follows:
    Qt Code:
    1. bool programMain::serverConnect() {
    2. //make the tunnel channel display:
    3. tunnel.setProcessChannelMode(QProcess::SeparateChannels);
    4. tunnel.setReadChannel(QProcess::StandardOutput);
    5. //spawn the SSH tunnel:
    6. tunnel.start("c:/progra~1/PuTTY/plink.exe -l user -ssh -pw password -C -v -N -L 9999:111.111.111.111:9999 remotecomputer.domain.com");
    7. //open SSH tunnel
    8. if (!tunnel.waitForStarted(20000)) {
    9. QMessageBox::critical(0, qApp->tr("Could not open SSH Tunnel"),
    10. qApp->tr("Unable to create secure ssh tunnel!\n\n")+ qApp->tr("\n\nClick Cancel to exit."), QMessageBox::Cancel,
    11. QMessageBox::NoButton);
    12. return false;
    13. }
    14. //hold the database login until the tunnel has responded with a login message:
    15. if (!tunnel.waitForReadyRead(20000)) {
    16. QMessageBox::critical(0, qApp->tr("SSH connection not responding!"),
    17. qApp->tr("SSH response timed out!\n\n")+ qApp->tr("\n\nClick Cancel to exit."), QMessageBox::Cancel,
    18. QMessageBox::NoButton);
    19. return false;
    20. }
    21.  
    22. //connect to the server:
    23. if (!createConnection(login.serveraddy, login.databasename, login.username, login.password, login.port)) {
    24. return false;
    25. }
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 

    The results are as follows:

    -If I run the code as-is, it will give me the "SSH connection not responding!" message, because for some reason ReadyRead never is triggered.

    -If i remove the "if (!tunnel.waitForReadyRead(20000)) {" piece, then it creates the ssh tunnel fine, however it does not wait to try to connect to the database after plink is started *and* plink is done connecting, so the database connection fails.

    This was what I was hoping to accomplish with the "if (!tunnel.waitForReadyRead(20000)) {" code, to make it wait till plink spit out the login successful text, and then continue on to log in to MySql.

    if I change "tunnel.setProcessChannelMode(QProcess::SeparateCh annels);" to "tunnel.setProcessChannelMode(QProcess::ForwardedC hannels);", i can see the terminal showing the login process going through fine, so I can only assume that there is some problem with the wait code.

    I could put some kind of timer to wait until most likely the tunnel should be created, but I would like the program to start logging in to mysql as soon as it can after ssh is done, for the best user experience.

    any tips on how I could do this?

  2. #2
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess, plink, mysql

    Basically what I think I need to figure out, is how do I read the output of the process properly. If I can read what plink outputs, and then do some handling with it then I would be ok, but I am not totally understanding how to:
    1) tell if a QProcess is sending any output
    2) how to capture that output

  3. #3
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess, plink, mysql

    If tunnel is an instance of QProcess then you can read from/write to it as with any QIODevice.

  4. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess, plink, mysql

    tunnel is a QProcess.

    do you have an example of reading from/writing to a QProcess? (reading from the process is what i'm most interested in) It seems most of the examples here are using start detached rather than actually controlling the process.

  5. #5
    Join Date
    Sep 2008
    Posts
    60
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess, plink, mysql

    In the attached example I use QTextStream::readAll() for simplicity. You can pass a process name from as a command line argument, /bin/ls is a default value.
    Attached Files Attached Files

  6. The following user says thank you to yuriry for this useful post:

    tpf80 (10th October 2008)

  7. #6
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess, plink, mysql

    Thanks for the helpful code, although i did not solve my problem quite yet, It seems that I am close.

    I compiled the example you gave me on a test machine and put in:
    Qt Code:
    1. QString cmd = "cmd.exe";
    2. m_p->start(cmd);
    To copy to clipboard, switch view to plain text mode 
    as a test and I got "Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp." to spit out, so I know your code is working how expected.

    I then changed the code to run plink as follows:
    Qt Code:
    1. QString cmd = "c:/progra~1/PuTTY/plink.exe -l username -ssh -pw \"password\" -C -v -N -L 9999:111.111.111.111:9999 server.somewhere.com";
    2. m_p->start(cmd);
    To copy to clipboard, switch view to plain text mode 

    what is interesting, is that when I use plink in the QProcess, the program spits out no text. I do know plink is working though because I can see in the task manager that process.exe and plink.exe are running, and the ssh tunnel is working. If I type that command on the command line, i get a very verbose chunk of text output from plink.

    So, it seems my problem is that for whatever reason, plink's output does not trigger the "readyReadStandardOutput()" signal in QProcess. Any clue as to why that could happen?

  8. #7
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QProcess, plink, mysql

    ah I found it!

    plink for some reason writes all it's output to the error channel. so changing "readyReadStandardOutput()" to "readyReadStandardError()" for the signal, made it work.

    thanks again for your help!

Similar Threads

  1. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  2. mysql configuration with qt
    By bala in forum Installation and Deployment
    Replies: 3
    Last Post: 6th November 2007, 11:02
  3. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 07:20
  4. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 13:23
  5. QProcess / system call not working under linux. Why?
    By johnny_sparx in forum Qt Programming
    Replies: 12
    Last Post: 11th March 2006, 00:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.