运行带有 html 按钮的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6235785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a shell script with an html button
提问by lemon
I want to launch a bash script when a button is pressed on a website. This is my first attempt:
我想在网站上按下按钮时启动 bash 脚本。这是我的第一次尝试:
<button type="button" onclick="/path/to/name.sh">Click Me!</button>
But no luck. Any suggestions?
但没有运气。有什么建议?
回答by nsg
As stated by Luke you need to use a server side language, like php. This is a really simple php example:
正如 Luke 所说,您需要使用服务器端语言,例如 php。这是一个非常简单的 php 示例:
<?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, ...
将其另存为myfilename.php
并将其放置在安装了 php 的 Web 服务器的机器上。同样的事情可以用 asp、java、ruby、python 来完成,......
回答by FreeSoftwareServers
This is really just an expansion of BBB's answer which lead to to get my experiment working.
这实际上只是 BBB 答案的扩展,导致我的实验工作。
This script will simply create a file /tmp/testfile when you click on the button that says "Open Script".
当您单击“打开脚本”按钮时,此脚本将简单地创建一个文件 /tmp/testfile。
This requires 3 files.
这需要3个文件。
- The actual HTML Website with a button.
- A php script which executes the script
- A Script
- 带有按钮的实际 HTML 网站。
- 一个执行脚本的 php 脚本
- 一个脚本
The File Tree:
文件树:
root@test:/var/www/html# tree testscript/
testscript/
├── index.html
├── testexec.php
└── test.sh
1. The main WebPage:
1.主网页:
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:
2. 运行脚本并重定向回主页面的 PHP 页面:
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 :
3. 脚本:
root@test:/var/www/html# cat testscript/test.sh
#!/bin/bash
touch /tmp/testfile
回答by BBB
PHP is likely the easiest.
PHP 可能是最简单的。
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:
只需制作一个script.php
包含<?php shell_exec("yourscript.sh"); ?>
单击按钮的人并将其发送到该目的地的文件即可。您可以将用户返回到带有标题的原始页面:
<?php
shell_exec("yourscript.sh");
header('Location: http://www.website.com/page?success=true');
?>
回答by Ashish Agarwal
There is a tutorial given over here. It will serve as a good starting point -
这里有一个教程。它将作为一个很好的起点——
http://www.cyberciti.biz/tips/executing-linuxunix-commands-from-web-page-part-i.html
http://www.cyberciti.biz/tips/executing-linuxunix-commands-from-web-page-part-i.html
回答by thom
You can do serverside scripting in bash without problem.
您可以毫无问题地在 bash 中进行服务器端脚本编写。
Here another tutorial: http://www.yolinux.com/TUTORIALS/BashShellCgi.html
回答by Tino Schr?ter
This is how it look like in pure bash
这就是它在纯 bash 中的样子
cat /usr/lib/cgi-bin/index.cgi
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>"