bash 从 HTML 执行 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44443164/
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
Execute a shell script from HTML
提问by john
I have written a simple HTML code that upon submit, it executes a bash script. The script is in the correct folder, (cgi-bin). When I load this page on firefox. It gives an error as
我编写了一个简单的 HTML 代码,在提交时,它会执行一个 bash 脚本。脚本位于正确的文件夹 (cgi-bin) 中。当我在 Firefox 上加载此页面时。它给出了一个错误
/somePATH/cgi-bin/script.sh could not be opened because an unknown error occurred.
Is the problem about script permissions? I have tried:
是脚本权限的问题吗?我试过了:
chmod 777
Here is a a part of the HTML code.
这是 HTML 代码的一部分。
<form action="cgi-bin/script.sh" method="post">
<input type="submit" value="Call my Shell Script">
</form>
EDIT: The script basically just prints the date:
编辑:脚本基本上只是打印日期:
#!/bin/bash
# get today's date
OUTPUT=$(date)
# script
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Demo</title></head><body>"
echo "Today is $OUTPUT <br>"
echo "You can do anything with this Schell Script"
echo "</body></html>"
回答by Aisiri Shankar
You can use a server side language. This is fairly easy with PHP
您可以使用服务器端语言。这对 PHP 来说相当容易
<?php
if(isset($_POST['submit']))
{
$output=shell_exec('sh /somePATH/cgi-bin/script.sh');
echo $output;
}
?>
<form action="" method="post">
<input type="submit" name="submit" value="Call my Shell Script">
</form>
Include all your other HTML and save the file with an extension .php
包含所有其他 HTML 并使用扩展名保存文件 .php
回答by Miguel Ortiz
Here is a list of things you could check in your httpd.conf regarding CGI:
以下是您可以在 httpd.conf 中检查的有关 CGI 的内容列表:
If you are loading or not the CGI module:
如果您正在加载或未加载 CGI 模块:
LoadModule cgi_module modules/mod_cgi.so
Verify your aliases for CGI
验证 CGI 的别名
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/etc/your_httpd_path/cgi-bin/"
The rules of your directory of scripts:
脚本目录的规则:
# "/etc/your_httpd_path/cgi-bin/" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/etc/your_httpd_path/cgi-bin/">
AllowOverride All
Options None
Require all granted
</Directory>
Handlers for different file extensions (add .sh)
不同文件扩展名的处理程序(添加 .sh)
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .cgi .pl .asp .sh
Check if everything is configured as expected in your environment.
检查是否在您的环境中按预期配置了所有内容。
Btw as a good practice: Do not give 777 to the script, give specific permissions for the apache user, find out which user is running the httpd service (it's usually www-data) and do something like:
顺便说一句,一个好的做法是:不要给脚本 777,为 apache 用户提供特定的权限,找出哪个用户正在运行 httpd 服务(通常是 www-data)并执行以下操作:
# remove extra file permissions for others
chmod o-wx /somePATH/cgi-bin/script.sh
# Define the user of the script (same as the user who runs httpd)
chown www-data /somePATH/cgi-bin/script.sh