0

I am trying to execute a python script via PHP. For some reason it doesn't execute. I have included the first line on my python code below:

#!/usr/bin/env python

Here is my PHP code that is executing this code (this is in index.php. test.py is in the same folder as this.):

if(isset($_GET["on"])){
    $command = escapeshellcmd("python test.py");
    shell_exec($command);
    echo "success";
}

But when I click the button to execute the script, I get "success" but the script doesn't execute. Any ideas why?

  • 1
    What does it output when you use `echo shell_exec($command);`? – Jirka Hrazdil Mar 03 '19 at 18:23
  • Nevermind, I just fixed this problem by accessing sudo visudo and adding this: www-data ALL=(ALL) NOPASSWD: ALL – localhost317 Mar 03 '19 at 18:28
  • you should **NOT** do that. if any attacker ever gets your php-scripts to execute arbitrary commands, they're executed "only" as www-data. but with your workaround, attackers can run commands as root - at which point the server is basically theirs. if you have to grant your webserver sudo-right, do it with a *very strict* whitelist of commands. – Franz Gleichmann Mar 03 '19 at 19:31

1 Answers1

0

If you are on linux, your script might not have correct privileges: http://php.net/manual/en/function.shell-exec.php#37971

try:

chmod +x test.py

source: https://stackoverflow.com/a/19736494/6337641

iseantx
  • 1
  • 1