Cannot Call Python script from PHP - php

I am trying to call a python script from PHP but not having any luck. I have searched for hours but found nothing. The python script is running just fine when I call it from the command line(connected to a relay switch, just runs through them, turning them on and off) and it works just fine. However, I can't seem to figure out how to get it to run from PHP. I am very new to PHP but here is what I am using:
<!doctype html>
<head>
<meta charset="UTF-8"/>
</head>
<?php
if(isset($_POST['switch'])){
exec("sudo python /home/pi/Desktop/test.py");
}
?>
<form method="post">
<button name="switch">Switch</button>
</form>
</html>
What am I doing wrong? I can't seem to find an answer anywhere that will make it work. The PHP is displaying the button just fine, but it does nothing when I click it.

shell_exec — Execute command via shell and return the complete output as a string . reference
<?php
if(isset($_POST['switch'])){
$c=escapeshellcmd("sudo python /home/pi/Desktop/test.py");
$res=shell_exec($c);
echo $res; // returns result to display
}
?>
in your script,output is not printed that may seem to not working
<?php
if(isset($_POST['switch'])){
$s=exec("sudo python /home/pi/Desktop/test.py");
echo "$s";
}
?>
add full path of interpreter in the first line of python script . if you have installed more than one python version
$s=exec("sudo -u /home/pi/Desktop/test.py"); this gives permission to python file
first of all make python file executable with chmod +x /path/to/python-script.py
EDIT:
from this post
You can't use sudo from a PHP script. Apache is running from an user (www-data generaly), so edit this file : /etc/sudoers
Then add this line :
www-data ALL=(ALL) NOPASSWD:ALL
Care ! this will authorize all functions to be called by a PHP script, you can adapt changing "ALL" by your script or Python command.
Then precise your user in your exec command :
<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/script.py')

Please make sure that the www user has the permission to execute your python script.
and then you should check if the system could find the PATH of the python libraries that you import in your python code.
I have the same experience with you, and I fixed the problem by checking the apache2 error_log, you'd better try, The error_log will tell what the real problem is !
cd /var/log/apache2
sudo more error.log
chmod 777 test.php
chmod 777 test.py
Good luck!

Related

Issues With PHP exec & shell_exec - Shell Script Execution

I've been unable to run php scripts that I need to use to start and stop webcam services that run on the local machine with the scripts. I can find nothing in the logs to indicate why the script doesn't' work.
I confess to being severely handicapped regarding PHP, especially server-side scripting.
The environment is Debian Jesse running Nginx with all required SSH and PHP modules installed
I have added www-data to the sudoers file with:
www-data ALL=(ALL) NOPASSWD: /var/www/html/start_webcam.sh
Enabled the $PATH environment for www-data at:
/etc/php5/fpm/pool.d/www.conf
The shell script resides in the .../html directory and runs from the terminal with no issues.
This is the code for both the php and shell scripts:
start_webcam.php:
<?php
echo exec('sudo bash /var/www/html/aspirebox/start_webcam.sh 2>&1, $output');
print_r($output);
?>
The $output and print_r stuff is there because it was the last thing I tried based on a post I found out here somewhere.
start_webcam.sh
#!/bin/bash
service motion start
Thanks in advance to anyone out here that has a clue. After 2 days of wrestling with this, I am sure that I do not.
according to Passing Variables to shell_exec()? you should change your code like this:
<?php
$output = exec('/var/www/html/aspirebox/start_webcam.sh 2>&1 ');
print_r($output);
?>
and let your bash script execute as all (no need to sudo bash):
chmod a+x /var/www/html/aspirebox/start_webcam.sh
Thank you very much - that worked.
I worked through getting the path straight for the directory the shell script runs in, and the correct path to run "service".
All I have now is to figure out why I'm getting "Failed to start motion.service: Access denied"
I've given www-data permission to run the script without a password on sudoers, have to keep digging.
Thanks again!

Running a Shell script from php

I have a question that have been asked many time over the web, but none of the solutions help my case.
I need a very simple website with a single button, by which on click, a single shell command is executed to deploy a docker container.
For this I have 3 files all located in /var/www/html/:
1- Depl-Script (a simple Linux file, made executable by chmod +x and chmod 777, and does not have .sh extension, including a single command:
docker run -it -p 8080:8080 surrogate
2- a DepGUI.php
<?php
// $output=shell-exec("/var/www/html/Depl-Script"); //didn't work
$output=shell_exec("./Depl-Script"); //this doesn't work neither
echo $output; //prints 0
$output1=shell_exec("whoami"); //prints www-data
echo $output1;
?>
3- a Main.html, including:
<form action="DepGUI.php">
<input type="submit" value="Open Script">
</form>
Shell-exec doesn't work for running the shell script.
How do I know it doesn't work? I check docker containers, nothing is created.
What solutions I have tried:
1) The shell script (Dep-Script) works fine when I run it from CLI, by root user.
2)Just to make sure, I changed the etc/passwd file, found the line with www-data user and changed it's login shell to /bin/bash, and then by www-data (apache) user, I executed the same shell script, and it works. (I returned the /etc/passwd file to the original shape)
3)I have checked the /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini and the exec, and shell_exec is not disabled.
4) I have checked the shell-exec and exec command with "sh" parameter, it did not work.
Any help/comment is appreciated in advance.
Thanks
Update:
I redirected errors to the output,
$output=shell_exec("./Depl-Script 2>&1");
I noticed the error is related to docker, "The input device is not a TTY" , then I got rid of -t in the docker command in the script (Dep-Script) and the error disappeared. I got a new error related to docker permissions and solved it using following link:
https://techoverflow.net/2017/03/01/solving-docker-permission-denied-while-trying-to-connect-to-the-docker-daemon-socket/

execute shell command with php

im trying to execute a bash script with a php/html button to wake my nas.
<form action="" method="POST">
<input type="submit" value="Wake NAS" name="zero" />
</form>
<?php
if (isset($_POST["zero"])){
#echo "Hello World!";
shell_exec("/var/www/html/wakenas.sh &");
}?>
"Hello World" is printed when button is pressed.
but code won't be executed.
the wakenas.sh looks like this and works if i execute it over shell
#!/bin/bash
etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1
wakenas.sh has all rights
Maybe you guys know why it wont be executed.
thanks in advance
The easy and secure way of executing your script is to put in sudoers. Assuming your Linux distribution is Debian base and user of who run the web server is www-data, then you can create a file e.g /etc/sudoers.d/wakeup_ether
Cmnd_Alias WAKE_UP_CMD = /var/www/html/wakenas.sh
www-data ALL=NOPASSWD: WAKE_UP_CMD
Modify your script to prefix the command with sudo.
shell_exec("sudo /var/www/html/wakenas.sh &");
Reference: https://help.ubuntu.com/community/RootSudo
From your dump:
etherwake: This program must be run as root.
when you execute wakenas.sh you probably are executing it as root. That's why it works.
Give the sudo permission (without password) to the user that your php server is running.
And change the wakenas.sh to:
#!/bin/bash
sudo etherwake -D "BC:5F:F4:09:E1:07"
echo "why!?!?!" > "/var/www/html/works.txt"
exit 1
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd("etherwake -D \"BC:5F:F4:09:E1:07\"");
//the return will be a string containing the return of the command
echo $return1;

Executing .exe file using PHP on Linux server

I'm new in using Linux, I'm trying to write a PHP code which can run .exe linux compatible file, I've made a short shell script
hello bash script:
#!/bin/bash
./program.exe file.mp4 // file.mp4 is an an input for .exe
echo "Hello World!"
shell.php:
<?php
$output = exec ("./hello ");
echo "<pre>$output</pre>";
?>
Now when I run shell.php using web browser it shows Hello World! but the .exe doesn't run, however when I run php using terminal command php shell.php, It works fine.
I think I'm having problems with permissions but I'm new with Linux and I don't know how to solve this.
Update:
I ignored the shell script and I used
<?php
$output = shell_exec ("cd /var/www/ && ./program.exe file.mp4 2>& " );
?>
also I granted access to program.exe
chmod 777 program.exe
the error I receive in the browser :could not open debug.bin!
use the absolute path to hello executable exec("sh path/to/the/file")
I'm using something similar to call an app compiled with mono on a remote ubuntu webserver and return it's output to the calling script.
For any of this to work properly wine needs to be already installed.
On Ubuntu systems try:
sudo apt-get -y install wine
You then need to know the owner of the web server process. If you are running the apache web server try the following:
cat /etc/apache2/envvars | grep "RUN"
The output will look something like this:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
export APACHE_RUN_DIR=/var/run/apache2$SUFFIX
Now that you have the name of the process owner, which in this case is www-data you should ensure the file is owned the user and its group:
sudo chown www-data /var/www/program.exe
sudo chgrp www-data /var/www/program.exe
Finally, we can invoke the application from inside our PHP script by passsing it as a parameter to 'wine' and using its full file path.
<?php
$output = shell_exec("wine /var/www/program.exe file.mp4" );
?>
Any output from the above shell command sent to the command line will be saved in the PHP script variable $output.
It looks like you are trying to do some output redirection with your use of program.exe file.mp4 2>& so I've left that off of the example for clairity.
Try using the absolute path, such as exec("sh /path/to/file")
Generally, php is run as www or apache, so make sure that the execute access permission is granted to all user.

How to start a program from web browers using php

I'm trying to make a open source kiosk like system. When the web browser starts all programs, it will run in the browser using PHP. I've found this link: Program execution Functions. It's using:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("WINWORD.exe", 7, false);
?>
I was able to start Microsoft Word on Windows, but I need to be able to do this on Linux.
This is what I've tried on my Linux server:
<?php
exec("/var/www/test.sh");
?>
But nothing happens. I know that test.sh works because I ran if from the terminal. I use test.sh to start a Python script. The Python script starts a text editor. I've tested the Python script and it works. All I need know is how to start the script from PHP on Linux.
Try out shell_exec.
shell_exec("/var/www/test.sh");
Also, make sure that the executable "permission" is set.
Additionally, you have to run the text editor as the logged-in user (propably you will do this with sudo or so) and to set the DISPLAY environment variable to :0.0 (or whatever is right for you). Example:
add this to /etc/sudoers:
ALL<tab>ALL=(kioskuser) NOPASSWD: ALL
(<tab> means that a real tab belongs there, edit the file by executing visudo as root)
content of the script:
#!/bin/bash
export DISPLAY=:0.0
sudo -u kioskuser /path/to/the/editor/command
I had to change the test.sh owner permission to www-data
then add
Cmnd_Alias RUN = /var/www/test.sh
www-data ALL = (root) NOPASSWD: RUN
To /etc/sudoers then the php code
<?php
shell_exec("sudo /var/www/test.sh");
?>

Categories