Results 1 to 2 of 2

Thread: read output of command running though ssh

  1. #1
    Join Date
    Jan 2017
    Posts
    4
    Platforms
    Unix/X11

    Wink read output of command running though ssh

    Hello Everyone,

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

    Qt Code:
    1. void Project::on_sshlogin_clicked()
    2. {
    3. QString loginfilename= "./path/to/script"; // path to login script
    4. QDesktopServices::openUrl(QUrl("file:///"+loginfilename,QUrl::TolerantMode));
    5. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void Project::on_sshlogout_clicked()
    2. {
    3. QProcess *logout = new QProcess();
    4. QString MainDirectory = "/path/to/script/directory";
    5. logout->setWorkingDirectory(MainDirectory);
    6. logout->start("/usr/bin/expect", QStringList() << "<nameofscript>.sh");
    7. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. controlon->setProcessChannelMode(QProcess::ForwardedChannels);
    To copy to clipboard, switch view to plain text mode 

    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!

    Ciao!
    Last edited by Cbr89; 30th January 2017 at 13:01. Reason: spelling corrections

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: read output of command running though ssh

    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 6
    Last Post: 29th November 2016, 15:27
  2. Start a linux command and read its standard output using QProcess
    By arunkumaraymuo in forum Qt Programming
    Replies: 3
    Last Post: 29th October 2015, 06:39
  3. Replies: 2
    Last Post: 28th May 2010, 10:16
  4. How to Get Konsole Command Output?
    By Furkan in forum Newbie
    Replies: 5
    Last Post: 14th April 2010, 21:16
  5. running shell command from C++ code problem
    By alex chpenst in forum Qt Programming
    Replies: 4
    Last Post: 31st July 2008, 11:41

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.