2

I have python script which I wan to load in my html via .php so, i have added this code in my test.php.

<?php
$command = escapeshellcmd("/var/www/html/ledon.py");
$output =  shell_exec($command);
echo $output;
?>

Shell_exec function works as if I put ls -l instead of $command it display all files. So I am not sure what I am doing wrong with escapeshellcmd function. Thanks.

soni8010
  • 385
  • 1
  • 6
  • 19
vsoni
  • 497
  • 1
  • 7
  • 22
  • 2
    does the script include a #! to tell it where to find python. If not you probably want to say "python /var/www/html/ledon.py". you might also want to make sure that the user that is running your webserver is able to access that file. – Paul Coldrey Jul 27 '17 at 04:54
  • I have updated my code with as you suggested python /var/www/html/ledon.py but it still gives me nothing. – vsoni Jul 27 '17 at 04:57
  • what happens if you type the command at a command prompt. Then what happens if you use su to become the user running apache and run it again? – Paul Coldrey Jul 27 '17 at 04:58
  • If i try to run the script from terminal with this command python ledon.py it works fine no issues there. But when i try to load it via .php it's not loading – vsoni Jul 27 '17 at 05:06
  • Possible duplicate of :https://stackoverflow.com/questions/19735250/running-a-python-script-from-php – Anoop Mayampilly Muraleedharan Jul 27 '17 at 05:23
  • Possible duplicate of [Running a Python script from PHP](https://stackoverflow.com/questions/19735250/running-a-python-script-from-php) – Anoop Mayampilly Muraleedharan Jul 27 '17 at 05:23

3 Answers3

0

With Python you have to invoke the interpreter by calling a script with 'python somescript.py'. Keeping in mind that I have very little knowledge of PHP, I would assume that you would need to do: $command = escapeshellcmd("python /var/www/html/ledon.py"); assuming that your shell has a link to wherever Python is installed.

Smitty
  • 1,765
  • 15
  • 22
0

From the official doc

Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

Python file should have correct privileges (execution for user www-data / apache if PHP script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.

You might want to use sudo and add an exception to your script to the sudoers file

NID
  • 3,238
  • 1
  • 17
  • 28
0

Your shell command is wrong. You need to specify which program to run on the file - i.e. to run it with Python, you need to run it with Python like so:

python /var/www/html/ledon.py

However, you also need to use system rather than shell_exec.

Thus, your full PHP code should be:

<?php
$command = escapeshellcmd("python /var/www/html/ledon.py");
$output =  system($command);
echo $output;
?>
Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55