简体   繁体   English

PHP Exec Sudo / Exim命令

[英]PHP Exec Sudo/Exim commands

I'm trying to get a sudo command to execute from within PHP. 我正在尝试从PHP内部执行sudo命令。 For simplicity sake, I'm using passthru wrapped in pre tags. 为了简单起见,我使用包装在预标签中的passthru。 Long story short, I want to get some exim stats. 长话短说,我想获得一些进出口数据。

I'm on a CentOS box. 我在CentOS盒子上。

After logging in via shell, I added the following line via visudo. 通过shell登录后,我通过visudo添加了以下行。

myuser      ALL = NOPASSWD: /usr/sbin/exiwhat

I can successfully execute the exiwhat command using either of the following commands when shelled in. 装入外壳时,可以使用以下两个命令之一成功执行exiwhat命令。

sudo -u myuser -H /usr/sbin/exiwhat
sudo -u myuser -H exiwhat

I've even tried becoming the the user via su and am still successful. 我什至尝试通过su成为用户,但仍然成功。

su myuser
sudo /usr/sbin/exiwhat
-or-
sudo exiwhat

My php script looks a little something like this. 我的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>';

All I get back is: 我得到的是:

myuser

1)
2)
3)
4)No exim process data

I've tried the same with 我已经尝试过

exec('exiwhat',$output);

but output returns as an empty array. 但是输出返回为空数组。

I can get simple things like "ps aux" or "dir" to work just fine. 我可以使“ ps aux”或“ dir”之类的简单内容正常工作。 Please help. 请帮忙。

Possible Duplicate: How to call shell script from php that requires SUDO? 可能重复: 如何从需要SUDO的php调用shell脚本?

Note: I can run exiwhat via command line and get a good chunk of output. 注意:我可以通过命令行运行exiwhat并获得大量输出。 We have several hundred emails in the queue at any point in time. 在任何时间点,我们都有几百封电子邮件。

UPDATE: Per another thread, I added " 2>&1" to each command and got the following 更新:在另一个线程中,我向每个命令添加了“ 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

UPDATE: I added the following line via visudo and my sudo commands now work 更新:我通过visudo添加了以下行,我的sudo命令现在可以正常工作

Defaults:myuser !requiretty

Is that save to leave like that though? 那是那样省钱吗?

Edit your /etc/sudoers file and remove the line about Default requiretty. 编辑您的/ etc / sudoers文件,并删除有关Default requiretty的行。

**Update I guess this options enabled by default so add something like the following instead: **更新,我猜默认情况下会启用此选项,因此请添加以下内容:

Defaults:myuser        !requiretty

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

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