PDA

View Full Version : read output of command running though ssh



Cbr89
30th January 2017, 12:59
Hello Everyone,

I have managed to execute bash scripts to log in to my raspberry using QDesktopServices:


void Project::on_sshlogin_clicked()
{
QString loginfilename= "./path/to/script"; // path to login script
QDesktopServices::openUrl(QUrl("file:///"+loginfilename,QUrl::TolerantMode));
}


This opens a terminal and executes the bash script, which logs into the RPi using 'expect' and can then administer commands.

I do not want to have a terminal window open. So I decided to use QProcess to run the script in the background:


void Project::on_sshlogout_clicked()
{
QProcess *logout = new QProcess();
QString MainDirectory = "/path/to/script/directory";
logout->setWorkingDirectory(MainDirectory);
logout->start("/usr/bin/expect", QStringList() << "<nameofscript>.sh");
}

I verify that this runs because I kill all active ssh sessions in the script.
So, I login and a terminal opens up and I can use the ssh terminal in the window. I click the logout button on my GUI and the Qprocess runs a similar script in the background and the initial ssh session is exited.

OK!!! So, I would like to make all both login and logout QProcesses as well, and have other buttons to perform commands.

First problem! I cannot get a reading of the output from the QProcess I have been trying to implement:


controlon->setProcessChannelMode(QProcess::ForwardedChannels) ;

However, I cannot get this to work. I would like to print the command entered, then the output of a normal command (such as ls,cd etc.). Just so I understand the basics.

Which brings me to my second problem! After I manage to do this I would like to monitor the output every second from a command that runs until exited. I will then extract values that I can use to control value displays on my GUI.

These values will be set by sending another command while the process is running.

Thank you for your help :) I will post any finished solution because I believe that this is more useful than random lines of code which is all I can find.

I will seek a more elegant solution using libssh at a later date and provide this solution also, any tips for setting up a basic test with libssh for raspberry pi connection would be greatly appreciated!:D

Ciao!

jefftee
31st January 2017, 08:06
Hi, first of all, I'd use either QProcess::setProcessChannelMode with QProcess::SeparateChannels or QProcess::MergedChannels depending on whether you want to read stdout or stderr separately or combined. Then use the QProcess::readyReadStandardOutput and/or QProcess::readyReadStandardError depending on your choice of separate or merged output channels.

If you stick with QProcess::ForwardedChannels, then you must read stdout and stderr of your main process, etc.

Good luck.