0

I am currently working on a web page where I want to run a python script on Button click event. My python script is working properly in terminal but I am not able to call the same script from the PHP page.

Below is the code for my PHP page.

<html>
<head>
<meta charset="UTF-8" />
</head>


<? php
print_r($_POST);

if (isset($_POST['ON']))
{
exec("sudo python /home/pi/Desktop/IOT/script5.py");
}
if (isset($_POST['OFF']))
{
exec("sudo python /home/pi/Desktop/IOT/script5a.py");
}
if (isset($_POST['1']))
{
exec("sudo python /home/pi/Desktop/IOT/script1.py");
}
?>
<form method="POST">
<button name="ON" value="Submit">turn On</button>&nbsp;
<button name="OFF">turn Off</button><br>&nbsp;
<button name="1">First</button><br>

</form>
</html>
Sachin611
  • 25
  • 6

1 Answers1

0

you need to pass your password to run any command with sudo, you can use the argument:

-S          The -S (stdin) option causes sudo to read the password from
            the standard input instead of the terminal device.

You can use it like (replace with your actually password):

exec("echo <password> | sudo -S python /home/pi/Desktop/IOT/script1.py");

Find more here

fajuchem
  • 181
  • 2
  • 11