I'm setting up a Apache 2, PHP 7, Python 3.6.3 on my Raspberry pi (Ubuntu)
I want execute Python script using PHP.
I've tried execute below code, but it does not work.
This is my body tag.
con_LED.php:
<body>
<form method="post">
<button name="LightON" value="on">LightON</button>
</form>
<form method="post">
<button name="LightOFF" value="off">LightOFF</button>
</form>
<p></p>
<?php
if (isset($_POST["LightON"])) {
exec("python3 /var/www/html/LEDON.py", $output, $var);
print_r($output."<br>");
print_r($var);
}
if (isset($_POST["LightOFF"])) {
exec("python3 /var/www/html/LEDOFF.py");
print_r($output);
print_r($var);
}
?>
</body>
I did some some troubleshooting.
To check, I replace my code to:
exec("ls -al", $output, $var)
Which works.
I also made:
a.py:
with open("t1.txt","a") as f:
f.write("Heelo\n")
And executed:
exec("python3 /var/www/html/a.py", $output, $var);
This also works (meaning I can see t1.txt file on my server).
My project is using con_LED.php code to turn the LED on and off.
But my code isn't working.
Of course, I also tried this code directly on shell, which is working.
Does anyone know what my problem is?
Related
I have an account on Gator.
I am trying to run a php script as follows
;$command = escapeshellcmd('./simple.py 2>&1');
$output = shell_exec($command);
echo $output
I would like to get as output anything that the python script prints to screen, but the output is empty no matter what I try (I use standard print in python
Use my method as a reference.
I have this two files
run.php
mkdir.py
Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.
run.php
<html>
<body>
<head>
<title>
run
</title>
</head>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python /var/www/html/lab/mkdir.py");
echo"success";
}
?>
mkdir.py
#!/usr/bin/env python
import os
os.makedirs("thisfolder");
$command = escapeshellcmd('./simple.py 2>&1');
escapeshellcmd is a form of surrender, it's php stupidity, it gets used as a DWIM band-aid when the programmer has lost control of the command-line.
It's breaking your stderr redirection.
Use escapeshellarg where you need to escape shell stuff.
$command = escapeshellarg('./simple.py') . ' ' . '2>&1';
I have a problem, I need to launch a .sh script from a web page, just does not go, starting from the terminal the script works and does what it has to do, but from the web no, but in the ' Apache2 error.log does not make any mistakes, I do not understand what it can be ..
HTML:
<tr>
<td>JTS</td>
<td>
<form action="JTSstart.php">
<input type="submit" value="START">
</form>
</td>
<td>
<form action="JTSres.php">
<input type="submit" value="RESTART">
</form>
</td>
<td>
<form action="JTSstop.php">
<input type="submit" value="STOP">
</form>
</td>
</tr>
PHP:
<?php
echo exec('bash JTSstop.sh');
sleep(5);
header("Location: 5ondimba.html");
?>
SH:
#!/bin/bash
cd /home/otaku/JTS3ServerMod_HostingEdition
./jts3servermod_startscript.sh stop
What I have tried / tested:
1) The exec command, such as shell_exec, is not disabled in the php setup.
2) The files were converted with dos2unix.
3) bash -x on the script and does not report any kind of error (in fact, from console works).
what could it be?? how can i make it work?
Thanks so much!
I see a few problems:
1) Get rid of echo. At best, it will display output of the command (if there is any), but if that happens it will cause your subsequent header() to fail - header() must be called before any output is generated.
2) PATHs are not the same in PHP as in your SHELL on the console. Don't assume bash or JTSstop.sh are in PHP's PATH, or in PHP's current working directory. Better to always fully specify paths.
3) Your script already includes #!/bin/bash, no need to call it with bash again.
PHP:
<?php
exec('/full/path/to/JTSstop.sh');
sleep(5);
header("Location: 5ondimba.html");
?>
But why not keep things simple and get rid of JTSstop.sh all together?
<?php
exec('cd /home/otaku/JTS3ServerMod_HostingEdition; ./jts3servermod_startscript.sh stop');
sleep(5);
header("Location: 5ondimba.html");
?>
If you're still having problems, you can see the results of the exec by specifying a 2nd parameter, as described in the docs.
<?php
exec('cd /home/otaku/JTS3ServerMod_HostingEdition; ./jts3servermod_startscript.sh stop', $output);
print_r($output);
I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.
I have some basic knowledge of HTML/PHP. The situation I am facing is frustrating. What I want to accomplish is to create a simple search box on a web page, when the user puts in input and clicks submit then my shell script is executed and then presented on a php page. I have been successful in getting other commands to run when I click submit to make sure the PHP exec shell command is working.
I will see the output on the web page. Just not my script. My script uses an argument to pass and works thru command line. Below is the details of my script, HTML, and PHP page. Also, I'm using a FreeBSD 10 box.
My Script
Command Line -
$ csearch "argument"
#!/bin/sh
grep -ir -B 1 -A 4 "$*" /usr/local/var/rancid/CiscoDevices/configs
My HTML page
<html>
<body>
<form method="POST" action="csearch.php">
<input type="text" name="searchText">
<input type="submit" value="Search">
</form>
</body>
</html>
My PHP page
<?php
$searchText=$_POST['$searchText'];
?>
<html>
<?php
$output = shell_exec('/usr/local/bin/csearch $searchText');
echo "<pre>$output</pre>";
?>
</html>
Any help is greatly appreciated.
shell_exec('/usr/local/bin/csearch $searchText'); This isn't what you expect it to be:
<?php
$searchText = 'foobar';
$cmd = '/usr/local/bin/csearch $searchText';
echo $cmd;
?>
outputs:
/usr/local/bin/csearch $searchText
Change the string to use double quotes and $searchText will actually be what you want to it be:
$output = shell_exec("/usr/local/bin/csearch $searchText");
More info on the use of quotes in PHP.
As #uri2x hints in a comment:
$searchText=$_POST['$searchText']; should be changed to $searchText=$_POST['searchText']; for a similar reason.
I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>
I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D