2015-05-21 69 views
0

我想調用一個python腳本,它具有正確的執行權限,但腳本包含一些只有root權限的命令。那麼我怎樣才能確保這些命令從webservice正確運行?使用exec執行php()

我跟着這個鏈接:Running a Python script from PHP但無法理解如何做到這一點。如果有人能解釋,這將是一個很大的幫助。

+0

可能重複[?如何運行PHP的exec()爲根(http://stackoverflow.com/questions/1598231/how-to-run-php-exec-as-root) – marijnz0r

+0

有關如何以不同用戶身份執行的更多詳細信息,請運行'man sudo'。 –

+0

@EJTH這是我的代碼:http://stackoverflow.com/questions/30367282/executing-python-script-from-php/30367638#30367638 –

回答

0

我會給你關於在任何地方靠近PHP的常見警告。我只是這樣做,因爲你提到這是一個web服務(面向公衆?)。

我最近發佈了一個項目,該項目允許PHP獲取並與真正的Bash shell進行交互(如果需要,用戶名爲apache/www-data或root)。在這裏獲得:https://github.com/merlinthemagic/MTS

下載您只需使用下面的代碼後:

//Setting the second argument in getShell(): 
//true will return a shell with root 
//false will return a shell with the php execution user 
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true); 
$return1 = $shell->exeCmd('python /full/path/to/python_script.py');