如何在Qt中执行复杂的linux命令? [英] How to execute complex linux commands in Qt?

查看:1060
本文介绍了如何在Qt中执行复杂的linux命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 QProcess 在linux中运行命令来重新启动计算机。我在我的应用程序中硬编码我的根密码。



当我在终端运行以下操作完美:


$ b b

  echo myPass | sudo -S shutdown -r now 

当我把命令放在shell脚本中并通过 QProcess 它也是成功的:

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

但我不能通过直接传递到 QProcess

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

它将只打印 myPass | sudo -S shutdown -r now



如何直接使用 QProcess 。 c>

QProcess


void QProcess :: setProcessChannelMode(ProcessChannelMode mode)



void QProcess :: setStandardOutputProcess(QProcess * destination)


因此,以下代码段等于 command1 | command2 ,而不限制一个解释器或另一个:

  QProcess process1 
QProcess process2;

process1.setStandardOutputProcess(& process2);

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

//等待它启动
if(!process1.waitForStarted())
return 0;

bool retval = false;
QByteArray buffer;
//公平:你只需要在这里等待一个关闭,
//但我仍然会留下一个通用的解决方案
while((retval = process2 .waitForFinished()));
buffer.append(process2.readAll());

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

您可以删除 sudo -S 部分,因为你可以作为root运行这个小程序,以及设置权限。您甚至可以为关闭程序设置setuid或setcap。



在构建商业Linux系统时,我们通常会做的最小的应用程序可以获得setuid或setcap活动,然后我们在Linux上用 system(3) QProcess 显式调用它。基本上,



我会编写这个小应用程序,以避免给整个应用程序的完全根访问,所以限制访问权限对恶意使用,如下:

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


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).

解决方案

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

这篇关于如何在Qt中执行复杂的linux命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆