Run a shell script with an html button
As stated by Luke you need to use a server side language, like php.This is a really simple php example:
<?phpif ($_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<?phpshell_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/bashtouch /tmp/testfile
PHP is likely the easiest.
Just make a file script.php
that contains <?php shell_exec("yourscript.sh"); ?>
and send anybody who clicks the button to that destination. You can return the user to the original page with header:
<?phpshell_exec("yourscript.sh");header('Location: http://www.website.com/page?success=true');?>