小烦 發表於 2022-8-12 15:35:00

麒麟系统下,qt以root启动外部程序或文件

<p>学习这位博主的:https://blog.csdn.net/shihoongbo/article/details/122290472。</p>
<p><span style="font-size: 16px"><strong>第一种方法:不用输入root密码</strong></span></p>
<p>echo模拟密码输入<br>创建sh脚本</p>
<p>#!/bin/bash<br>echo '' | sudo -S <br><br>PS: sudo -S 是大写S<br>sudo 的-S参数,将从标准输入中读取密码</p>
<p>这种方式,模拟键盘输入密码进行sudo管理员权限。<br>缺点非常明显,必须提前知道管理员密码,而且在后续的使用过程中一旦密码变换就失效。完全不是民用Linux程序的方式<br>一般只用作Linux进阶用户避免繁琐sudo操作的方法</p>
<p><strong><span style="font-size: 16px">&nbsp;第二种:会弹出个框,让手动输入root密码</span></strong></p>
<p>pkexec (桌面环境)<br>创建sh脚本</p>
<p>#!/bin/bash<br>pkexec <br><br>桌面弹出密码输入框,正确后获得管理员权限执行command<br>这种方式,可以直接弹出类似Ubuntu、Fedora一些Linux操作系统的输入密码的窗口,只有正确输入才能启动后续的命令。<br>一般情况下,这种方法足以应对大部分民用Linux程序的管理员权限获取。<br><br></p>
<p>以上方法,</p>
<p>第一种很好实现:sh脚本文件里:写如:echo 'rootpwd' | sudo -S ./AppRun&nbsp; &nbsp;像这样就行。当然这种方式,用代码也可以,很不错</p>
<p>代码像这样:</p>
<div class="cnblogs_code">
<pre>QProcess *backupProcess = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QProcess;
    backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">setWorkingDirectory(path);

    connect(backupProcess, SIGNAL(error(QProcess::ProcessError)), </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, SLOT(sendProcessError(QProcess::ProcessError)));
QString pwd</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">rootpwd</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    backupProcess</span>-&gt;start((<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">sh -c \"echo '</span><span style="color: rgba(128, 0, 0, 1)">"</span>+pwd+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">' | sudo -S ./AppRun</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).toLatin1());
    backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">waitForStarted();   
    </span><span style="color: rgba(0, 0, 255, 1)">bool</span> isfinished = backupProcess-&gt;<span style="color: rgba(0, 0, 0, 1)">waitForFinished();

    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(isfinished){
      QTextCodec</span>* pTextCodec = QTextCodec::codecForName(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">System</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      QByteArray qba </span>= backupProcess-&gt;<span style="color: rgba(0, 0, 0, 1)">readAllStandardError();
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">output: </span><span style="color: rgba(128, 0, 0, 1)">"</span> &lt;&lt; pTextCodec-&gt;toUnicode(backupProcess-&gt;<span style="color: rgba(0, 0, 0, 1)">readAllStandardOutput());
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StandardError: </span><span style="color: rgba(128, 0, 0, 1)">"</span> &lt;&lt;<span style="color: rgba(0, 0, 0, 1)"> QString::fromLocal8Bit(qba);
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StandardError1: </span><span style="color: rgba(128, 0, 0, 1)">"</span> &lt;&lt; pTextCodec-&gt;<span style="color: rgba(0, 0, 0, 1)">toUnicode(qba);
      backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">close();
      delete backupProcess;
      backupProcess </span>=<span style="color: rgba(0, 0, 0, 1)"> NULL;
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">exec linux Cmd finished</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    }</span></pre>
</div>
<p>&nbsp;</p>
<p>第二种方法试了,不行不知道咋回事,也弹出窗口了,输入密码退出了。<br>第二种方式这样的,我之前在代码里实现过;代码如下(里面path+filename是要执行的脚本文件地址)</p>
<div class="cnblogs_code">
<pre>QProcess *backupProcess = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QProcess;
    backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">setWorkingDirectory(path);

    connect(backupProcess, SIGNAL(error(QProcess::ProcessError)), </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, SLOT(sendProcessError(QProcess::ProcessError)));
    backupProcess</span>-&gt;start(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">pkexec /bin/bash</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">waitForStarted();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">backupProcess-&gt;start("pkexec",QStringList() &lt;&lt; path + fileName);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">backupProcess-&gt;start("pkexec echo \"root\" &gt; /boot/grub/test.txt");
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">backupProcess-&gt;write("echo \"root\" &gt; /boot/grub/test.txt");</span>
    QString str=QString(path+fileName+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    qDebug() </span>&lt;&lt;<span style="color: rgba(0, 0, 0, 1)"> str.toStdString().c_str();
    backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">write(str.toStdString().c_str());
    </span><span style="color: rgba(0, 0, 255, 1)">bool</span> isfinished = backupProcess-&gt;<span style="color: rgba(0, 0, 0, 1)">waitForFinished();

    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(isfinished){
      QTextCodec</span>* pTextCodec = QTextCodec::codecForName(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">System</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      QByteArray qba </span>= backupProcess-&gt;<span style="color: rgba(0, 0, 0, 1)">readAllStandardError();
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">output: </span><span style="color: rgba(128, 0, 0, 1)">"</span> &lt;&lt; pTextCodec-&gt;toUnicode(backupProcess-&gt;<span style="color: rgba(0, 0, 0, 1)">readAllStandardOutput());
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StandardError: </span><span style="color: rgba(128, 0, 0, 1)">"</span> &lt;&lt;<span style="color: rgba(0, 0, 0, 1)"> QString::fromLocal8Bit(qba);
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">StandardError1: </span><span style="color: rgba(128, 0, 0, 1)">"</span> &lt;&lt; pTextCodec-&gt;<span style="color: rgba(0, 0, 0, 1)">toUnicode(qba);
      backupProcess</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">close();
      delete backupProcess;
      backupProcess </span>=<span style="color: rgba(0, 0, 0, 1)"> NULL;
      qDebug() </span>&lt;&lt; <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">exec linux Cmd finished</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    }</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/dachuang/p/16580205.html
頁: [1]
查看完整版本: 麒麟系统下,qt以root启动外部程序或文件