tpf80
8th October 2008, 10:44
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:
bool programMain::serverConnect() {
//make the tunnel channel display:
tunnel.setProcessChannelMode(QProcess::SeparateCha nnels);
tunnel.setReadChannel(QProcess::StandardOutput);
//spawn the SSH tunnel:
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");
//open SSH tunnel
if (!tunnel.waitForStarted(20000)) {
QMessageBox::critical(0, qApp->tr("Could not open SSH Tunnel"),
qApp->tr("Unable to create secure ssh tunnel!\n\n")+ qApp->tr("\n\nClick Cancel to exit."), QMessageBox::Cancel,
QMessageBox::NoButton);
return false;
}
//hold the database login until the tunnel has responded with a login message:
if (!tunnel.waitForReadyRead(20000)) {
QMessageBox::critical(0, qApp->tr("SSH connection not responding!"),
qApp->tr("SSH response timed out!\n\n")+ qApp->tr("\n\nClick Cancel to exit."), QMessageBox::Cancel,
QMessageBox::NoButton);
return false;
}
//connect to the server:
if (!createConnection(login.serveraddy, login.databasename, login.username, login.password, login.port)) {
return false;
}
return true;
}
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::SeparateCha nnels);" to "tunnel.setProcessChannelMode(QProcess::ForwardedCh annels);", 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?
bool programMain::serverConnect() {
//make the tunnel channel display:
tunnel.setProcessChannelMode(QProcess::SeparateCha nnels);
tunnel.setReadChannel(QProcess::StandardOutput);
//spawn the SSH tunnel:
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");
//open SSH tunnel
if (!tunnel.waitForStarted(20000)) {
QMessageBox::critical(0, qApp->tr("Could not open SSH Tunnel"),
qApp->tr("Unable to create secure ssh tunnel!\n\n")+ qApp->tr("\n\nClick Cancel to exit."), QMessageBox::Cancel,
QMessageBox::NoButton);
return false;
}
//hold the database login until the tunnel has responded with a login message:
if (!tunnel.waitForReadyRead(20000)) {
QMessageBox::critical(0, qApp->tr("SSH connection not responding!"),
qApp->tr("SSH response timed out!\n\n")+ qApp->tr("\n\nClick Cancel to exit."), QMessageBox::Cancel,
QMessageBox::NoButton);
return false;
}
//connect to the server:
if (!createConnection(login.serveraddy, login.databasename, login.username, login.password, login.port)) {
return false;
}
return true;
}
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::SeparateCha nnels);" to "tunnel.setProcessChannelMode(QProcess::ForwardedCh annels);", 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?