Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem sending command to terminal with QProcess
QtWS25 Last Chance

Problem sending command to terminal with QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
qprocessfedoraterminalcommandnewbie
6 Posts 3 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcasey627
    wrote on 2 Apr 2016, 02:16 last edited by
    #1

    I am currently using Fedora 23 and QT 5.6. I am building a command for Hping3 and trying to send that command to the terminal and receive the results upon a button push. I can get the terminal to open but I can not get it to receive any commands. I am very new to QT so my knowledge is minimal. I learned programming with Visual Basic so I understand logic but this syntax is killing me.

    Qt Code:

    //Sending the command to terminal
        QProcess proc;
        proc.start("/usr/bin/gnome-terminal");
     
        proc.waitForStarted();
        proc.write("su");
        proc.write("ite");
        //proc.write(command);
        proc.waitForFinished();
     
        QByteArray output = proc.readAll();
        proc.close();
        ui->dgvResults->setText(output);
    

    I am trying to display in a QTextBrowser and Commented out that one write command because I am getting an error

    /home/itestudent/SeniorProject/frmmain.cpp:112: error: no matching function for call to 'QProcess::write(QString&)'
    proc.write(command);
    ^

    Command is declared at the top of the button push event
    QString command = "hping3 -S ";

    Any help would be great

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Apr 2016, 10:14 last edited by
      #2

      hi and welcome
      the write seems to want a const char * so try with
      p.write(command .toStdString().c_str());

      ( you give it QString& and it dont want a QString)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 2 Apr 2016, 22:57 last edited by
        #3

        Hi and welcome to devnet,

        Since you only want the result, are you sure you need the gnome-terminal ? Or is to enter su's password ?

        Use QByteArray for command in place of QString.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply 3 Apr 2016, 20:58
        0
        • S SGaist
          2 Apr 2016, 22:57

          Hi and welcome to devnet,

          Since you only want the result, are you sure you need the gnome-terminal ? Or is to enter su's password ?

          Use QByteArray for command in place of QString.

          D Offline
          D Offline
          dcasey627
          wrote on 3 Apr 2016, 20:58 last edited by
          #4

          @SGaist I was trying to get into the su because sudo was not working. Now i figured out how to fix sudo so I am trying to send echo ite | sudo -S hping3 -S -c 5 <portNumber> <IPAdress> to the bash. My code waits like it is processing the command but I can not get it to update the text box(dgvResults). If i change the text box(dgvResults) to a simple string it will output that though. The code that I have so far is

          QProcess proc;
          proc.start("/bin/bash");

              proc.waitForStarted();
              proc.execute(command);
              proc.waitForFinished();
          
              QByteArray output = proc.readAll();
              proc.close();
              ui->dgvResults->setText(output);
          
          1 Reply Last reply
          0
          • D Offline
            D Offline
            dcasey627
            wrote on 3 Apr 2016, 21:15 last edited by
            #5

            I just want to thank you for the help. I just found another thread that had exactly what I needed. I swear I tried this before and it didn't work but somehow it does now

            QProcess proc;
            proc.start("/bin/bash", QStringList() << "-c" << command);

                proc.waitForFinished();
            
                QByteArray output = proc.readAll();
                proc.close();
                ui->dgvResults->setText(output);
            

            Again thank you for the responses they were very helpful

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 3 Apr 2016, 21:30 last edited by
              #6

              Glad you found out and thanks for sharing !

              Since you have it working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              4/6

              3 Apr 2016, 20:58

              • Login

              • Login or register to search.
              4 out of 6
              • First post
                4/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved