How to execute complex linux commands in Qt?

Nejat

I want to restart the computer by running a command in linux using QProcess. I have hard-coded my root password in my application.

When i run the following in a terminal it works perfect:

echo myPass | sudo -S shutdown -r now 

When i put the command in a shell script and call it via QProcess it is also successful :

QProcess process;
process.startDetached("/bin/sh", QStringList()<< "myScript.sh");

But i can not run it by directly passing to QProcess:

process.startDetached("echo myPass | sudo -S shutdown -r now ");

It will just print myPass | sudo -S shutdown -r now

How is it possible to run such relatively complex commands directly using QProcess. (Not putting in a shell script).

lpapp

The key methods that exist for this purpose established in QProcess:

void QProcess::setProcessChannelMode(ProcessChannelMode mode)

and

void QProcess::setStandardOutputProcess(QProcess * destination)

Therefore, the following code snippet would be the equivalence of command1 | command2 without limiting yourself to one interpreter or another:

QProcess process1
QProcess process2;

process1.setStandardOutputProcess(&process2);

process1.start("echo myPass");
process2.start("sudo -S shutdown -r now");
process2.setProcessChannelMode(QProcess::ForwardedChannels);

// Wait for it to start
if(!process1.waitForStarted())
    return 0;

bool retval = false;
QByteArray buffer;
// To be fair: you only need to wait here for a bit with shutdown,
// but I will still leave the rest here for a generic solution
while ((retval = process2.waitForFinished()));
    buffer.append(process2.readAll());

if (!retval) {
    qDebug() << "Process 2 error:" << process2.errorString();
    return 1;
}

You could drop the sudo -S part because you could run this small program as root, as well as setting up the rights. You could even set setuid or setcap for the shutdown program.

What we usually do when building commercial Linux systems is to have a minimal application that can get setuid or setcap for the activity it is trying to do, and then we call that explicitly with system(3) or QProcess on Linux. Basically,

I would write that small application to avoid giving full root access to the whole application, so to restrict the access right against malicious use as follows:

sudo chmod u+s /path/to/my/application

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute "export" command in linux via Qt

From Dev

How to execute "export" command in linux via Qt

From Dev

How to execute linux commands in a remote machine with shell script

From Dev

How to execute parallel commands

From Dev

How to execute linux commands from R via bash under the Windows Subsystem for Linux (WSL)?

From Dev

How does bash execute commands

From Dev

How to execute mysql commands in sh

From Dev

How to execute two commands sequentially

From Java

Execute combine multiple Linux commands in one line

From Dev

Execute Commands in the Linux Commandline [Lazarus / Free Pascal]

From Dev

Execute commands on linux remotely through Dropbox ?

From Dev

Execute three commands in single command line in Linux

From Dev

Limit user to execute selective commands (Linux)

From Dev

Login to remote Linux box and execute commands

From Dev

How can I execute an external commands in C/Linux without using system, popen, fork, exec?

From Dev

How to execute a php script in Qt?

From Dev

Complex table with qt - how do I start?

From Dev

How to execute git commands from bash script?

From Java

How to execute mongo commands through shell scripts?

From Dev

Clojure: how to execute shell commands with piping?

From Dev

How to execute bash commands from C?

From Java

How to execute a group of commands as another user in Bash?

From Dev

How to read commands from a file and execute them?

From Dev

How to execute multiple CLI commands in a python script?

From Dev

How to use SystemWorker to execute PowerShell commands?

From Dev

How to easily execute R commands on remote server?

From Dev

How to execute terminal commands through scala

From Dev

How to execute the vim commands through shell script

From Dev

How to execute mongo commands from bash?

Related Related

  1. 1

    How to execute "export" command in linux via Qt

  2. 2

    How to execute "export" command in linux via Qt

  3. 3

    How to execute linux commands in a remote machine with shell script

  4. 4

    How to execute parallel commands

  5. 5

    How to execute linux commands from R via bash under the Windows Subsystem for Linux (WSL)?

  6. 6

    How does bash execute commands

  7. 7

    How to execute mysql commands in sh

  8. 8

    How to execute two commands sequentially

  9. 9

    Execute combine multiple Linux commands in one line

  10. 10

    Execute Commands in the Linux Commandline [Lazarus / Free Pascal]

  11. 11

    Execute commands on linux remotely through Dropbox ?

  12. 12

    Execute three commands in single command line in Linux

  13. 13

    Limit user to execute selective commands (Linux)

  14. 14

    Login to remote Linux box and execute commands

  15. 15

    How can I execute an external commands in C/Linux without using system, popen, fork, exec?

  16. 16

    How to execute a php script in Qt?

  17. 17

    Complex table with qt - how do I start?

  18. 18

    How to execute git commands from bash script?

  19. 19

    How to execute mongo commands through shell scripts?

  20. 20

    Clojure: how to execute shell commands with piping?

  21. 21

    How to execute bash commands from C?

  22. 22

    How to execute a group of commands as another user in Bash?

  23. 23

    How to read commands from a file and execute them?

  24. 24

    How to execute multiple CLI commands in a python script?

  25. 25

    How to use SystemWorker to execute PowerShell commands?

  26. 26

    How to easily execute R commands on remote server?

  27. 27

    How to execute terminal commands through scala

  28. 28

    How to execute the vim commands through shell script

  29. 29

    How to execute mongo commands from bash?

HotTag

Archive