That will not works, you have to create an action for that:
<?php
if (isset($_POST['button']))
{
exec('test.pl');
}
?>
<html>
<body>
<form method="post">
<p>
<button name="button">Run Perl</button>
</p>
</form>
</body>
Answer from tttony on Stack OverflowThat will not works, you have to create an action for that:
<?php
if (isset($_POST['button']))
{
exec('test.pl');
}
?>
<html>
<body>
<form method="post">
<p>
<button name="button">Run Perl</button>
</p>
</form>
</body>
Looks like you are trying to call PHP with a JavaScript action. This will not work. You can try submitting a form and executing the PHP code when the form is submitted, like:
<?php if (isset($_POST['button'])) { exec('test.pl'); } ?>
<form action="" method="post">
<button type="submit" name="button">Run Perl</button>
</form>
Exec a PHP function from a Form button? - PHP - SitePoint Forums | Web Development & Design Community
Running a shell script from an html button - Raspberry Pi Forums
linux - Execute shell commands through an html button(onclick ) - Stack Overflow
Run a shell script with an html button - Stack Overflow
As stated by Luke you need to use a server side language, like php. This is a really simple php example:
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
exec("/path/to/name.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>
Save this as myfilename.php and place it on a machine with a web server with php installed. The same thing can be accomplished with asp, java, ruby, python, ...
This is really just an expansion of BBB's answer which lead to to get my experiment working.
This script will simply create a file /tmp/testfile when you click on the button that says "Open Script".
This requires 3 files.
- The actual HTML Website with a button.
- A php script which executes the script
- A Script
The File Tree:
root@test:/var/www/html# tree testscript/
testscript/
├── index.html
├── testexec.php
└── test.sh
1. The main WebPage:
root@test:/var/www/html# cat testscript/index.html
<form action="/testscript/testexec.php">
<input type="submit" value="Open Script">
</form>
2. The PHP Page that runs the script and redirects back to the main page:
root@test:/var/www/html# cat testscript/testexec.php
<?php
shell_exec("/var/www/html/testscript/test.sh");
header('Location: http://192.168.1.222/testscript/index.html?success=true');
?>
3. The Script :
root@test:/var/www/html# cat testscript/test.sh
#!/bin/bash
touch /tmp/testfile
What you are trying to do is not possible that way.
Note that there are always two sides to that: The client side and the server side. Is the script on the client computer or on the server?
If it's on the client: You as the visitor are only seeing an HTML website. onClickwill only be able to launch JavaScript (or other scripting languages), but not any arbitrary shell script that resides on your computer. HTML scripts only run in the browser and can only do limited things. Most importantly, they can't interact with your computer.
Think about it: How would the browser know how to open the file? Don't you think this would be a security issue as well – a plain website triggering the execution of scripts on a client's computer? What if there was something like onClick('rm -rf /home/user')?
An alternative would be to run a Java applet, if you want code to be executed on the client, but this not exactly the same and it's something really complicated. I don't think it's necessary to explain this in detail.
If the script is on the server: If you want to run a script on the server side and have the user trigger its execution, then you need to use a server side programming language. Just HTML won't do it, because it's more or less a static file. If you want to interact with the server, you could for example use PHP.
It has the exec function to run a command line script that is stored on the web server. So basically, you could write exec('/path/to/name.sh'); and it would run the script on the server.
However, just putting this into onClick is not enough here. If you don't know about PHP and server side web programming yet, you might want to read a few tutorials first and then come back with a more specific question.
If you have a php file with the appropriate exec(...) command, make sure the script has execute permissions set not only for the user but also for the group the web server is in, so in the simplest case just 777.
In case of trouble check for the return value of the script with echo exec(...); to see if there are any errors.
You can also run the script from the command line and not from the browser with php /path/to/file.php.
You need some server side intelligence for this. HTML alone is not enough, because it is static. One common way would be php. Many hosted offers do have php installed by default.
You can use an ftp program to put a text file into the root directory of your server.
The textfile could be named "run.php" with the following content:
<h3>Executing /path/to/name.sh</h3>
<?
exec('/path/to/name.sh');
?>
Let's say your domain is "example.com", if you visit this page in your browser:
http://example.com/run.php
then the php file will be executed on the server. It will send an HTML page to the browser with the heading. And the script will be executed on the server.
There are some things to keep in mind and some possible improvements:
1) everybody will be able to hit this page, also robots. You could secure the page with htacces.
2) This page will fire on case of a normal "GET" request from the browser. But it involes an action on the server, and if this action changs data or does something important, it would be better to only fire the script on a POST request.
3) You can insert a form / button statement to be able to reload / execut again the page. Be sure to use the right method (GET or POST) in the method attribute of the HTML form statement.
4) It would be good to get the result of the shell script (rturn code and maybe text output) and write it to the browser. This has enough issues for a separate qustion : )
It could just be a PATH problem since the user www-data doesn't have a path set. Try using:
<?
exec("/usr/bin/python /var/www/open.py")
?>
and see if that makes a difference (check that /usr/bin/python is the correct path for your distribution first with which python).
Could 2 paths to resolution:
fetch http://my.site.com/action.php directly and confirm that code execures that way. In case of some PHP issues.
SELinux: if your SELinux is set to "enforcing", try temporary disabling it just to confirm that it's not in the way: "setenforcing Permissive". If that cures the issue - you know you need to adjust things. First and foremost I doubt any default policy would allow executable scripts in /var/www dir, check your distro's default policy and locate appropriate folder. /var/www/cgi-bin may be one of them, or possibly system-wide directory like /usr/local/bin etc.