Run a shell script with an html button - html

I want to launch a bash script when a button is pressed on a website.
This is my first attempt:
<button type="button" onclick="/path/to/name.sh">Click Me!</button>
But no luck. Any suggestions?

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 -->
Click Me!
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

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:
<?php
shell_exec("yourscript.sh");
header('Location: http://www.website.com/page?success=true');
?>
Reference: http://php.net/manual/en/function.shell-exec.php

This is how it look like in pure bash
cat /usr/lib/cgi-bin/index.cgi
#!/bin/bash
echo Content-type: text/html
echo ""
## make POST and GET stings
## as bash variables available
if [ ! -z $CONTENT_LENGTH ] && [ "$CONTENT_LENGTH" -gt 0 ] && [ $CONTENT_TYPE != "multipart/form-data" ]; then
read -n $CONTENT_LENGTH POST_STRING <&0
eval `echo "${POST_STRING//;}"|tr '&' ';'`
fi
eval `echo "${QUERY_STRING//;}"|tr '&' ';'`
echo "<!DOCTYPE html>"
echo "<html>"
echo "<head>"
echo "</head>"
if [[ "$vote" = "a" ]];then
echo "you pressed A"
sudo /usr/local/bin/run_a.sh
elif [[ "$vote" = "b" ]];then
echo "you pressed B"
sudo /usr/local/bin/run_b.sh
fi
echo "<body>"
echo "<div id=\"content-container\">"
echo "<div id=\"content-container-center\">"
echo "<form id=\"choice\" name='form' method=\"POST\" action=\"/\">"
echo "<button id=\"a\" type=\"submit\" name=\"vote\" class=\"a\" value=\"a\">A</button>"
echo "<button id=\"b\" type=\"submit\" name=\"vote\" class=\"b\" value=\"b\">B</button>"
echo "</form>"
echo "<div id=\"tip\">"
echo "</div>"
echo "</div>"
echo "</div>"
echo "</div>"
echo "</body>"
echo "</html>"
Build with https://github.com/tinoschroeter/bash_on_steroids

Related

Make a html button open a website on the server side [duplicate]

I want to launch a bash script when a button is pressed on a website.
This is my first attempt:
<button type="button" onclick="/path/to/name.sh">Click Me!</button>
But no luck. Any suggestions?
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 -->
Click Me!
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
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:
<?php
shell_exec("yourscript.sh");
header('Location: http://www.website.com/page?success=true');
?>
Reference: http://php.net/manual/en/function.shell-exec.php
This is how it look like in pure bash
cat /usr/lib/cgi-bin/index.cgi
#!/bin/bash
echo Content-type: text/html
echo ""
## make POST and GET stings
## as bash variables available
if [ ! -z $CONTENT_LENGTH ] && [ "$CONTENT_LENGTH" -gt 0 ] && [ $CONTENT_TYPE != "multipart/form-data" ]; then
read -n $CONTENT_LENGTH POST_STRING <&0
eval `echo "${POST_STRING//;}"|tr '&' ';'`
fi
eval `echo "${QUERY_STRING//;}"|tr '&' ';'`
echo "<!DOCTYPE html>"
echo "<html>"
echo "<head>"
echo "</head>"
if [[ "$vote" = "a" ]];then
echo "you pressed A"
sudo /usr/local/bin/run_a.sh
elif [[ "$vote" = "b" ]];then
echo "you pressed B"
sudo /usr/local/bin/run_b.sh
fi
echo "<body>"
echo "<div id=\"content-container\">"
echo "<div id=\"content-container-center\">"
echo "<form id=\"choice\" name='form' method=\"POST\" action=\"/\">"
echo "<button id=\"a\" type=\"submit\" name=\"vote\" class=\"a\" value=\"a\">A</button>"
echo "<button id=\"b\" type=\"submit\" name=\"vote\" class=\"b\" value=\"b\">B</button>"
echo "</form>"
echo "<div id=\"tip\">"
echo "</div>"
echo "</div>"
echo "</div>"
echo "</div>"
echo "</body>"
echo "</html>"
Build with https://github.com/tinoschroeter/bash_on_steroids

How to update header.phtml with hardcoded image

I am trying to remove an image from a header.phtml file. It has been hard coded into the header.phtml in addition to the regular magento header image. I cannot access the image I want to delete through magento. I have found the line of code where it has been added to the header.phtml but don't know what the next step is to change this... can I delete the header.phtml file from our sftp and then re-upload it with the appropriate line of code deleted or will that ruin the whole site?
Your help is very much appreciated! I am a novice. HTML good.. everything else clueless but definitely know the line of code that has to go.
Thank you!
Maybe you add the echo function " ";
For example
<table border = "3">
     <tr>
         <td>
             <? php
             $ files = glob ("img / *");
             foreach ($ files as $ file) {
                 echo "<div class = 'divimages'>";
                 echo '<img src = "'. $ file. '" />';
                 echo "<input type = 'submit' value = 'Delete image' /> <br>";
                 echo "</ div>";
             }
             ?>
         </ td>
     </ tr>
</ table>

Input argument to bash cgi from html

I have searched this on google at least 5 times and have not obtained an answer.
I want to have an HTML form with a text input that inputs an argument to my CGI script.
Apologies in advance, I have bad spelling and don't know how to use this site well.
Here is my code:
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo '<!DOCTYPE html>'
echo '<html><head>'
echo '<title>Template</title>'
echo '<meta charset="UTF-8">'
echo '<meta name="viewport" content="width=device-width, initial-scale=1">'
echo '</head><body>'
echo '<h1>Template</h1>'
echo '<div class="main">'
echo '<pre>'
ls -la
echo '</pre>'
echo '<form action="index.cgi">'
echo '<input type="text">'
echo '<input type="submit" name="submit" name="ls_path">'
echo '</form>'
echo '</div>'
echo '</body></html>'
exit 0
Clinery,
It is unclear from your question if you are having trouble getting the webserver to run your CGI script, or if you are having trouble processing the input box content in bash.
For the former please make sure your webserver configuration is ok, an example for apache can be found here.
For the later please look at the last example "Process input from an HTML form" here

HTML Anchor to calendar?

Is there a way to make a generic link to create a calendar event as an HTML anchor's URI? For instance, you can make a link that creates an email with Link text, and you can make a link that initiates a phone call with Link text. Is there such a syntax for calendars that is NOT program specific? (I know there are things like outlook:// and iCal can use .ics files, but I want a program-agnostic one)
I'm looking for something like <a href="cal:Event%20title?start=timestamp&end=timestamp&description=Arbitrary%20description%20text>Link text</a>
You'll need to use a combination of the webcal link as well as a dynamically created ics file. Usage of webcal would be as follows:
Link
However, for best compatibility, I think your best bet would be to just link directly to the ics file:
Link
Then, in the createCal.php file (or whichever your programming language of choice is) you could do something similar to this article to create an ics file on the fly that the user could import into their calendar program of choice.
Here's another sample I found for the contents of the PHP file (note that this isn't tested, but more so a starting point):
<?php
//Set the content-type of the file
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=Calendar.ics");
echo "BEGIN:VCALENDAR\n";
echo "PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN\n";
echo "VERSION:2.0\n";
echo "METHOD:PUBLISH\n";
echo "X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n";
echo "BEGIN:VEVENT\n";
echo "CLASS:PUBLIC\n";
echo "CREATED:".date('Ymd\THis', time())."\n";
echo "DESCRIPTION:".$_GET['description'];
echo "DTEND:".date('Ymd\THis', $_GET['end'])."\n";
echo "DTSTAMP:".date('Ymd\THis', time())."\n";
echo "DTSTART:".date('Ymd\THis', $_GET['start'])."\n";
echo "LAST-MODIFIED:".date('Ymd\THis', time())."\n";
echo "LOCATION:\n";
echo "PRIORITY:5\n";
echo "SEQUENCE:0\n";
echo "SUMMARY;LANGUAGE=en-us:".$_GET['title']."\n";
echo "TRANSP:OPAQUE\n";
echo "UID:040000008200E00074C5B7101A82E008000000008062306C6261CA01000000000000000\n";
echo "X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n";
echo "X-MICROSOFT-CDO-IMPORTANCE:1\n";
echo "X-MICROSOFT-DISALLOW-COUNTER:FALSE\n";
echo "X-MS-OLK-ALLOWEXTERNCHECK:TRUE\n";
echo "X-MS-OLK-AUTOFILLLOCATION:FALSE\n";
echo "X-MS-OLK-CONFTYPE:0\n";
//Here is to set the reminder for the event.
echo "BEGIN:VALARM\n";
echo "TRIGGER:-PT1440M\n";
echo "ACTION:DISPLAY\n";
echo "DESCRIPTION:Reminder\n";
echo "END:VALARM\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>

Back button on the image preview

I have a minor problem, i just want a simple image preview with a go back button or something similar, I made
<div id="image1">
<a href="../assets/image_large1.jpg"><img src="../assets/image_1.jpg" width="160" height="107" alt="Example 1" />
</div>
but I don't know how to put a button inside
how could I do it?
In the most simple solution to your problem, I would do special show_image.php page, for example:
<?php
$directory = 'assets/';
$filename = $_GET['image'];
if(empty($filename) || is_file( $directory . $filename ))
{
echo 'No such image!';
}
else
{
echo '<img src="' . $directory . $filename . '" />';
}
echo '<br />';
echo 'Go back';
exit;
And link for the image will be: http://mysite.com/show_image.php?image=image_1.jpg
Of course this code should be made more secure and this thing could be done other ways.