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.
Related
I'm trying to execute a shell script from an HTML page using PHP. I have found a previous example here that I have been trying to follow but I'm having an issue. I'm not sure what the cause is but no errors are returned and no file is created from the bash script.
index.php:
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form action="./test.php">
<input type="submit" value="Open Script">
</form>
</body>
</html>
test.php
<?php
exec("./bash_script.sh");
header('Location: http://local.server.edu/ABC/abc_test/');
?>
bash_script.sh
#!/bin/bash
touch ./test_file.txt
One thing I have noticed that may be the cause of the issue is that it seems the path on the local server doesn't match with the file system exactly.
If I switch all the relative paths in the scripts to absolute paths such as:
/local/sequence/temp/abc_test/file.exe
Then after clicking the button to run the script I get an error saying:
The requested URL /local/sequence/temp/abc_test/test.php was not found on this server
Edit:
The three files They're located at /local/sequence/temp/abc_test And there is a symbolic link pointing to that directory at /export/www/htdocs/ABC
The error message seems to be indicating that test.php is not being found. As written, it needs to be in the same directory as index.php
You’ve tested the actual bash script, so we can proceed with the assumption that it’s in the execution of the script receiving the submission.
I would suggest putting all the web stuff into one page, because you can test sending and receiving input.
<?php
// for testing
// exec("./bash_script.sh");
// check for POST submission (this is not just reading data)
if(isset($_POST['runScript'])) {
// die('Request received');
exec("./bash_script.sh");
// It’s always proper to redirect after post :)
header('Location: http://local.server.edu/ABC/abc_test/');
die;
}
// finished with logic; show form
?>
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form method="POST">
<input type="submit" name="runScript" value="Open Script">
</form>
</body>
</html>
Note that I added the name attribute to the submit button, and made the form use POST method while submitting to the calling page (no action means submit to yourself).
I’ve also left a few commented actions to aid in debugging if necessary
You may have to adjust the path to the bash script. Currently it’s going to look in the same directory as index.php, which is not something you’d want to do in production.
You will be able to do that somehow, but its always very risky to allow such operations to execute from the php page.
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 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
I am looking for a way to have like a button in a web page, only when you press it the event handling is a program in a server. Like a trigger to execute a program in the server.
Is there any way to do this?
A PHP script can execute a command on the server using one of the various functions like exec, system, and passthru. It can be a security nightmare, so be careful.
You might want to look into something like: http://php.net/manual/en/book.exec.php
AJAX for the client part (sending notification about the button being pressed), and PHP (function exec() ou system() ).
Like the others have said, exec, system, and passthru can be used for that, but I believe you will need to disable safe mode.
safe_mode = Off
http://php.net/manual/en/features.safe-mode.php
To add on to this answer, you can make a form
Page1.html
<html>
<head>
</head>
<body>
<form action="script.php" method="post">
<input type="submit" />
</form>
</body>
</html>
script.php
<?php
shell_exec("cmd");
echo "done";
?>
Someone correct me if I am wrong here.