简体   繁体   English

在Qt QProcess中运行sudo命令

[英]Run sudo command in Qt QProcess

I am developing a software in Qt in which I created a terminal. 我正在Qt开发一个软件,我在其中创建了一个终端。 I run different commands through QProcess in that but when I run root commands it ask for password in terminal. 我通过QProcess运行不同的命令,但是当我运行root命令时,它在终端中要求输入密码。 I tried to run via sudo but it only accepts password in terminal. 我试图通过sudo运行,但它只接受终端密码。 Is there any way to give password from another source like pop up widget or a text file? 有没有办法从其他来源提供密码,如弹出窗口小部件或文本文件?

You could try 你可以试试

  • Running your application as root (which is really a very bad idea, actually!) 以root身份运行您的应用程序(这实际上是一个非常糟糕的主意!)
  • Edit sudoers file and add the commands you want to run to this file. 编辑sudoers文件并将要运行的命令添加到此文件中。 Then you can run these commands like sudo run_x_cmd with no password ie, your QProcess can run these commands and you won't be asked for password. 然后你可以运行这些命令,如sudo run_x_cmd ,没有密码,即你的QProcess可以运行这些命令,你不会被要求输入密码。

I have created a QProcess with "bash" as program. 我用“bash”作为程序创建了一个QProcess。

Then just write to it: 然后写信给它:

echo mypassword | sudo -S ifconfig eth0 192.168.1.123\n

Adding a password to a text file in order to source input for the command is a very bad idea, as it weakens security. 将密码添加到文本文件以获取命令的输入是一个非常糟糕的主意,因为它会削弱安全性。

Version 1.8 of sudo provides a plugin architecture, which would allow you to link to it from your application and may provide a solution for you. sudo的1.8版本提供了一个插件架构,允许您从应用程序链接到它,并可以为您提供解决方案。

The SDK for the sudo plugin API can be found in the documentation . 可以在文档中找到sudo插件API的SDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM