0

I am trying to call a python script from php which have proper execution permission but the script contains some of the commands for which only the root has the permission. So how can i make sure that those commands runs properly from the webservice???

I followed this link: Running a Python script from PHP but could not understand how to do it. If someone could explain, it will be a great help.

Community
  • 1
  • 1
  • possible duplicate of [How to run PHP exec() as root?](http://stackoverflow.com/questions/1598231/how-to-run-php-exec-as-root) – marijnz0r May 21 '15 at 12:21
  • run `man sudo` for more details on how to execute as a different user. – boaz_shuster May 21 '15 at 12:36
  • @EJTH This is my code: http://stackoverflow.com/questions/30367282/executing-python-script-from-php/30367638#30367638 – Zareen Arshad May 21 '15 at 16:25
  • That is not your code, that is someone elses code. Also instead of using `exec('python foo.py')` try the absolute path to your python executable (Use `which python` if you are running NIX to determine where your python binary is) – EJTH May 22 '15 at 07:05
  • _It is a not convenient situation, and for sure, a security architecture issue in your system_. **A solution can be to reduce the need of executing something secured as root** using the webserver user, for example, by changing the permissions on the resources (if possible). Otherwise, **you could allow the webserver user to run on those "certain" root protected resources** by adding a user group in which your webserver and root users are included. – Evhz May 18 '16 at 12:25

1 Answers1

0

I will give you the usual warning regarding having PHP anywhere near root. I only do this because you mention this is a webservice (public facing?).

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//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');
MerlinTheMagic
  • 575
  • 5
  • 16