duk*_*vin 17 html shell button
我想在网站上按下按钮时启动bash脚本.这是我的第一次尝试:
<button type="button" onclick="/path/to/name.sh">Click Me!</button>
Run Code Online (Sandbox Code Playgroud)
但没有运气.有什么建议?
小智 23
正如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>
Run Code Online (Sandbox Code Playgroud)
将其保存为myfilename.php并将其放置在安装了php的Web服务器的计算机上.使用asp,java,ruby,python等可以完成同样的事情......
小智 6
这就是它在纯 bash 中的样子
猫 /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>"
Run Code Online (Sandbox Code Playgroud)
使用 https://github.com/tinoschroeter/bash_on_steroids构建
PHP可能是最简单的.
只需创建一个script.php包含<?php shell_exec("yourscript.sh"); ?>并将任何点击该按钮的人发送到该目的地的文件.您可以将用户返回到包含标题的原始页面:
<?php
shell_exec("yourscript.sh");
header('Location: http://www.website.com/page?success=true');
?>
Run Code Online (Sandbox Code Playgroud)
参考:http://php.net/manual/en/function.shell-exec.php
实际上,这只是BBB答案的扩展,可以使我的实验正常进行。
当您单击显示“打开脚本”的按钮时,此脚本将仅创建文件/ tmp / testfile。
这需要3个文件。
文件树:
root@test:/var/www/html# tree testscript/
testscript/
??? index.html
??? testexec.php
??? test.sh
Run Code Online (Sandbox Code Playgroud)
1.主网页:
root@test:/var/www/html# cat testscript/index.html
<form action="/testscript/testexec.php">
<input type="submit" value="Open Script">
</form>
Run Code Online (Sandbox Code Playgroud)
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');
?>
Run Code Online (Sandbox Code Playgroud)
3.脚本:
root@test:/var/www/html# cat testscript/test.sh
#!/bin/bash
touch /tmp/testfile
Run Code Online (Sandbox Code Playgroud)