【发布时间】:2014-05-17 04:35:06
【问题描述】:
我正在尝试从 PHP 中执行 sudo 命令。为简单起见,我使用包含在 pre 标签中的 passthru。长话短说,我想获得一些进出口统计数据。
我在 CentOS 机器上。
通过shell登录后,我通过visudo添加了以下行。
myuser ALL = NOPASSWD: /usr/sbin/exiwhat
我可以在shell中使用以下任一命令成功执行exiwhat命令。
sudo -u myuser -H /usr/sbin/exiwhat
sudo -u myuser -H exiwhat
我什至尝试通过 su 成为用户并且仍然成功。
su myuser
sudo /usr/sbin/exiwhat
-or-
sudo exiwhat
我的 php 脚本看起来有点像这样。
<?php
echo '<pre>';
passthru('whoami');
echo "\n1)";
passthru('sudo exiwhat');
echo "\n2)";
passthru('sudo /usr/sbin/exiwhat');
echo "\n3)";
passthru('exiwhat');
echo "\n4)";
passthru('/usr/sbin/exiwhat');
echo "\n";
echo '</pre>';
我得到的只是:
myuser
1)
2)
3)
4)No exim process data
我也试过了
exec('exiwhat',$output);
但输出返回一个空数组。
我可以让“ps aux”或“dir”等简单的东西正常工作。请帮忙。
可能重复:How to call shell script from php that requires SUDO?
注意:我可以通过命令行运行 exiwhat 并获得大量输出。我们随时都有数百封电子邮件在队列中。
更新: 对于另一个线程,我在每个命令中添加了“2>&1”并得到以下内容
myuser
1)sudo: sorry, you must have a tty to run sudo
2)sudo: sorry, you must have a tty to run sudo
3)sh: exiwhat: command not found
4)/bin/rm: cannot remove `/var/spool/exim/exim-process.info': Permission denied
exim(770): Operation not permitted
exim(8016): Operation not permitted
exim(15618): Operation not permitted
exim(15626): Operation not permitted
exim(16751): Operation not permitted
exim(16765): Operation not permitted
exim(32207): Operation not permitted
exim: no process killed
No exim process data
更新: 我通过 visudo 添加了以下行,我的 sudo 命令现在可以工作了
Defaults:myuser !requiretty
这样就可以离开了吗?
【问题讨论】:
-
进一步说明:运行 mod_suphp,因此每个进程都应该以各自的用户身份运行,希望如 whoami 命令所示。