Web page does not run shell script - php

I've made a simple webpage that has to run some simple bash scripts. I run apache2 on Jetson Nano and I have three files in /var/www/html folder:
index.html
testexec.php
test.sh
First one looks like:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<form action="testexec.php">
<input type="submit" value="Create file">
</form>
</body>
</html>
the php file:
<?php
$output = shell_exec("test.sh");
header('Location: http://192.168.25.16/index.html?success=true');
?>
script:
#!/bin/bash
touch file.txt
My problem is that everything looks good, but the scrit doesn't run. In future it will be used to run program written in python, but for now I can't run even that simple one. Is the problem with location of files or with something else?
I've already tried to change php file
$output = shell_exec("test.sh");
with
$output = exec("test.sh");
with or without $output
My browser (firefox) returns no errors in console.
Script works fine when I run it from shell. It is executable.
I've already tried to look for similar problems, but there were no solutions.

Very often the problem is with user rights, first try installing chmod 777 test.sh to just check if this is the problem.
In addition, you should go back to the exec documentation
exec(string $command, array &$output = null, int &$result_code = null): string|false
and check what $result_code is equal to.
If we get echo result!:
$output=null;
$retval=null;
exec('echo $(pwd);', $output, $retval);
echo "${retval} with value:\n";
print_r($output);

Related

Run python code from PHP page on beaglebone

I'm trying to run the code bellow from a PHP page on my beaglebone black:
import Adafruit_BBIO.PWM as PWM
red = "P8_13"
green = "P8_19"
blue = "P9_14"
PWM.start(red, 0)
PWM.start(blue, 0)
PWM.start(green, 0)
PWM.set_duty_cycle(red, 100)
PWM.set_duty_cycle(green, 0)
PWM.set_duty_cycle(blue, 0)
This code is just to turn my RGB LED to red.
My PHP page bellow:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RGB LED</title>
</head>
<body>
Testing led.py execution...<br>
<?php
exec( "python led.py" );
?>
<br>End of execution!<br>
</body>
</html>
I'm using Apache2 and PHP5.
The code is on the same path of PHP page ("/var/www"). I already tried exec, shell_exec and system commands to execute the script. The LED doesn't turn to red. The code already have 777 permission. I really don't know why it isn't working. Does anyone have any idea?
Thank you.
I don't really want to go deep with this problem. But I am writing you this because I once played with a BBB and had a funny permission error while running python scripts. I was running commands without sudo. Please try it. I don't why it should have a sudo but it works!
Problem solved.
Thank you #furas.
I added "www-data ALL=(root) NOPASSWD: /usr/bin/python" to the sudoers file and also changed exec("python led.py") to exec ("sudo python led.py").

Trying to run a python script via PHP button hosted on raspberry pi but failing

I have a php script that should (I think) run a python script to control the energenie radio controlled plug sockets depending on which button is selected. It seems to work in that it echos back the correct message when the button is pressed but the python scripts don''t appear to run. I have added the line:
www-data ALL=NOPASSWD: /usr/bin/python /home/pi/lampon.py
which should give the apache user privileges to run the python script at least for turning on the power socket but it doesn't work. The script itself does work when run via the pi command line itself. Any suggestions? (the code for the php is below)
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
LED Control:
<form method="get" action="energenie.php">
<input type="submit" value="ON" name="on">
<input type="submit" value="OFF" name="off">
</form>
<?php
if(isset($_GET['on'])){
shell_exec("python /home/pi/lampon.py");
echo "LED is on";
}
else if(isset($_GET['off'])){
shell_exec("python /home/pi/lampoff.py");
echo "LED is off";
}
?>
</body>
</html>
Have you tried setting the permissions of the lampoff.py and lampon.py to 777?
chmod 777 /home/pi/lampoff.py && chmod 777 /home/py/lampon.py
I think you need add "sudo" the python script for it to work which means you have to add the www-data user to /etc/sudoers.
shell_exec("sudo python /home/pi/lampon.py");
or
exec("sudo python /home/pi/lampon.py");
There was another post that deal with this recently on Execute Python script from Php

HTML not showing characters

I'm running lighttpd 1.4.35 on Debian 8.2. I have a very simple html file with php code (php5) which calls a bash script and prints the output:
<html>
<body>
<?php
$res = shell_exec("/var/www/html/run.sh");
echo $res . " is the result";
?>
</body>
</html>
If that html file is called on firefox, the output is
is the result
If I directly run php with that file (php index.php), the output is
<html>
<body>
13.00
is the result</body>
</html>
So, where does the result get lost?
edit:
The webpage source code from firefox is
<html>
<body>
is the result</body>
</html>
edit: solved. bash script uses '~' which expands to wrong directory when the script is run from webserver.
The exec functions "only" return the contents of stdout, which is why you might miss an error message.
But you can redirect stderr to stdout, e.g. via
$res = shell_exec("/var/www/html/run.sh 2>&1");
shell_exec does not run if you're in safe mode, that could be the issue

Python script from php doesnt give any output

This is my php script
<HTML>
<head>
<h1>Hello</h1>
</head>
<body>
<?php
$command=escapeshellcmd('~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
$output=shell_exec($command);
echo $output;
?>
</body>
</HTML>
I get no output when I run it through the browser.
When I change the
$command=escapeshellcmd('whoami');
it outputs as NOBODY,
I granted permission to nobody for that python script still no output.
I am using xampp for linux and php script in htdocs/xampp.
Are you sure you don't need to change your command to run python?
$command=escapeshellcmd('python ~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
You also might try specifying absolute path - the ~ alias changes based on current user (which is the web server's user, not yours).

shell_exec() returning null on "ls"

So I have this code and I'm only trying to make a list of the saves in another directory where the php scrip is in xampp folder and the saves are to this path /root/files/saves:
<html>
<body>
<?php
$output = shell_exec('ls /root/files/saves');
echo "<pre>$output</pre>";
?>
</body>
</html>
I don't know why I can't get it working on a var_dump it seems output is null I'm really confuse it should work or I just it all wrong I need some help.
Add 2>&1 to the end of your shell command to have STDERR returned as well as STDOUT.
$output = shell_exec("ls /root/files/saves 2>&1");
Also, if the user running PHP doesn't have sufficient permissions to view the output in /root/, the above code will return a Permission denied error message.
Source: http://php.net/manual/en/function.shell-exec.php#28994

Categories