M
M
meridbt2020-09-09 10:31:01
PHP
meridbt, 2020-09-09 10:31:01

How to execute a Python script from PHP that writes to system files?

Good afternoon, dear colleagues.

I'm trying to make a page for changing network settings on RaspberryPI.
Created a script that writes settings to the dhcpcd.conf configuration:

if len (sys.argv) == 2:
    with open('/etc/dhcpcd.conf', 'a') as file:
        file.write(sys.argv[1])

If you run the script from the terminal, it runs correctly as the pi user.
If you run the script from PHP:
$command = escapeshellcmd('/usr/bin/python3 /var/www/html/assets/modules/test.py 764');
system($command);

then python returns dhcpcd.conf file access error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitsliputsli, 2020-09-09
@meridbt

Use the sudo mechanism. If pi can write to dhcpcd.conf then give www-data permission to run /usr/bin/python3 as user pi without a password.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question