2014-04-04 124 views
1

我試圖從PHP內部執行sudo命令。爲了簡單起見,我使用包裹在pre標籤中的passthru。長話短說,我想獲得一些exim統計數據。PHP Exec Sudo/Exim命令

我在CentOS盒子上。

通過shell登錄後,我通過visudo添加了以下代碼。

myuser  ALL = NOPASSWD: /usr/sbin/exiwhat 

在炮擊時,我能成功地執行使用下列命令的exiwhat命令。

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

我甚至試圖通過成爲蘇用戶現在也還是成功的。

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並得到輸出的很大一部分。在任何時候,我們都有幾百封電子郵件。

UPDATE: 每另一個線程,我加了 「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 

雖然這樣保存就離開了嗎?

+0

此外應注意:運行mod_suphp,所以每個過程應該作爲其各自用戶的身份運行,由WHOAMI有望指示命令。 – GameCharmer

回答

2

編輯你的/ etc/sudoers文件並刪除關於Default default requiretty的行。

**更新我想在默認情況下啓用該選項,以便添加類似以下代替:

Defaults:myuser  !requiretty 
+1

通過以下鏈接發現,並在您發佈之前獲知它。 http://serverfault.com/questions/111064/sudoers-how-to-disable-requiretty-per-user – GameCharmer