0

I would like to open a python script from php. My php user is "user23" which is the main user account on my Ubuntu 18.04 desktop. start.php:

    <?php 
    echo 'php user: ' . get_current_user();
    $output=shell_exec("python ./test.py");
    echo'<br>python script user: ';
    echo($output);
    echo '<br>Current PHP version: ' . phpversion();
    ?>

test.py:

    import getpass
    import os
    print(getpass.getuser())
    os.mkdir(getpass.getuser())

PHP output if i open http://localhost/start.php with chrome:

php user: user23
python script user: root
Current PHP version: 7.1.32

So the script works, but the 'root' folder is not created. PHP error log:

  File "./test.py", line 4, in <module>
    os.mkdir(getpass.getuser())
OSError: [Errno 13] Permission denied: 'root'

I do not understand why the php and the python users are different (user23,root) and how it is possible that the root user has no permission to create the folder (Errno 13] Permission denied: 'root').

if I open the start.php the python user will be user23 and the folder created perfectly.

$ php start.php
php user: user23<br>python script user: user23
<br>Current PHP version: 7.2.19-0ubuntu0.18.04.2

I use xampp if it's matter. I hope you can help me, I'm totally lost right now. Sorry for my english.

elemEZ
  • 3
  • 1

1 Answers1

0

maybe it help you php code with sudo


shell_exec('echo "PASS" | sudo -u root -S python .test.py');

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28