如何通过PHP访问root包?

时间:2015-01-03 09:23:15

标签: php shell root permission-denied

问题:最初,通过命令行作为root用户,我访问了一个名为 pandoc (/root/.cabal/bin/pandoc) 的软件包,该软件包安装在根文件夹中。当我尝试使用 shell_exec() 通过php访问该软件包时,它会失败。

问题:为了安全起见,php shell_exec() 是否有限制不访问root软件包?如果是,如何解决?

我试过:给根文件夹写权限然后我就可以访问root包了 命令行不是root用户。但我无法通过php shell_exec() 访问它。

php代码:

shell_exec("cd /home/quotequadsco/public_html/pandoc_jats ; sudo -u quotequadsco 
-S /root/.cabal/bin/pandoc ex.tex --filter /root/.cabal/bin/pandoc-citeproc
-t JATS.lua -o ex.xml");

并尝试过,

shell_exec("cd /home/quotequadsco/public_html/pandoc_jats ;/root/.cabal/bin/pandoc 
ex.tex --filter /root/.cabal/bin/pandoc-citeproc -t JATS.lua -o ex.xml");

期望:我需要在php中通过 shell_exec() 执行pandoc root包。

2 个答案:

答案 0 :(得分:0)

在/ etc / sudoer文件中添加了以下行

#Defaults requiretty   //commented this line
usergroup ALL=(ALL)  ALL

PHP代码,

shell_exec("cd /home/quotequadsco/public_html/pandoc_jats ;echo password | sudo 
-S command"); //added a password for sudo command to run as a root user.

答案 1 :(得分:0)

我最近发布了一个项目,允许PHP获取真正的Bash shell并与之交互(如果需要,可以作为root),它解决了exec()和shell_exec()的限制。在此处获取:https://github.com/merlinthemagic/MTS

下载后,您只需使用以下代码:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('pandoc (/root/.cabal/bin/pandoc)');
//the return will be a string containing the return of the command
echo $return1;