0

I need to run a python script before which I need to activate a virtual env for the script to run and it is in root folder. This is the command to do that (without using source command)

exec(root/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py).

how do I execute from apache. I get 126 error in php. I have given the actual path.

126 error means- "Command invoked cannot execute. A permission or command not executable problem.".

How do I do this. The actual path is in the root folder, I can't move it to outside of root.

<?php
  $op=exec('/root/Envs/ate/bin/python /var/www/cgi-bin/TStool     /box_upgrade.py',$output,$return);
  echo "Dir returned $return, and output:\n";
  var_dump($output);
  echo $return;  
  echo $op;
    ?>

Any suggestions? Thanks.

5
  • Is your web server running as root? If not, it probably can't access anything in the /root/ directory. Commented Mar 21, 2016 at 21:56
  • No it is running as apache. But I also tried moving the entire Envs/ate/bin/python folder to another folder and tried running the script but it gave return code 1(general error). Not sure what is the error. Commented Mar 21, 2016 at 22:14
  • Is there a reason you have to run this as PHP script instead of as machine-bootstrapping bash script or the like? Needing to exec from PHP is generally incredibly insecure. Commented Mar 21, 2016 at 22:27
  • The same script runs from console. Now I get a return code 1 in php. which means incorrect function. The command I gave is $op=exec('/var/www/cgi-bin/ate-activate/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py',$output,$return);i gave full 777 permissions on all the folders and scripts. Commented Mar 21, 2016 at 22:41
  • @Mike'Pomax'Kamermans_ I m creating an online tool, got my front end as html and backend as php from which I need to run python script to upgrade/downgrade a server. Commented Mar 21, 2016 at 22:42

1 Answer 1

1

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/root/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py');
//the return will be a string containing the return of the command
echo $return1;

In terms of security it is far better than running apache as root. But letting PHP anywhere near root is always tricky.

The project i built achieves a root bash shell in one of 2 ways:

1) You allow apache the right to sudo python.

OR

2) You pass root credentials to the object every time you need a shell with root setup.

Pick your poison. :) Read the documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.