Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从HTML执行shell脚本_Html_Bash_Shell - Fatal编程技术网

从HTML执行shell脚本

从HTML执行shell脚本,html,bash,shell,Html,Bash,Shell,我编写了一个简单的HTML代码,在提交时,它会执行一个bash脚本。 脚本位于正确的文件夹(cgi bin)中。 当我在firefox上加载此页面时。它给出了一个错误 /somePATH/cgi-bin/script.sh could not be opened because an unknown error occurred. 脚本权限是否存在问题?我试过: chmod 777 下面是HTML代码的一部分 <form action="cgi-bin/script.sh" metho

我编写了一个简单的HTML代码,在提交时,它会执行一个bash脚本。 脚本位于正确的文件夹(cgi bin)中。 当我在firefox上加载此页面时。它给出了一个错误

/somePATH/cgi-bin/script.sh could not be opened because an unknown error occurred.
脚本权限是否存在问题?我试过:

chmod 777
下面是HTML代码的一部分

<form action="cgi-bin/script.sh" method="post">
<input type="submit" value="Call my Shell Script">
</form>

编辑: 脚本基本上只打印日期:

#!/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>"
#/bin/bash
#得到今天的日期
输出=$(日期)
#剧本
echo“内容类型:文本/html”
回声“”
回声“演示”
echo“今天是$OUTPUT
” echo“你可以用这个Schell脚本做任何事情” 回声“”
以下是您可以在httpd.conf中查看有关CGI的内容列表:

如果您正在加载或未加载CGI模块:

LoadModule cgi_module modules/mod_cgi.so
验证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/"
脚本目录的规则:

# "/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>
检查您的环境中是否按预期配置了所有内容

顺便说一句,作为一个好的实践:不要给脚本777,给apache用户特定的权限,找出哪个用户正在运行httpd服务(通常是www数据),然后执行以下操作:

# 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

以下是您可以在httpd.conf中查看的有关CGI的内容列表:

如果您正在加载或未加载CGI模块:

LoadModule cgi_module modules/mod_cgi.so
验证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/"
脚本目录的规则:

# "/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>
检查您的环境中是否按预期配置了所有内容

顺便说一句,作为一个好的实践:不要给脚本777,给apache用户特定的权限,找出哪个用户正在运行httpd服务(通常是www数据),然后执行以下操作:

# 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

您可以使用服务器端语言。这对于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>


包含所有其他HTML并使用扩展名保存文件。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>


包括所有其他HTML,并使用扩展名
.php

保存文件。脚本是什么样子的?@ShammelLee在问题中编辑了
cgi-gin/script.sh
的内容是什么?另外,请指定您的服务器/配置。@ShammelLee我在问题中添加了该脚本。我正在本地主机上运行它,HTTP服务正在运行。您是否在脚本顶部指定了一个shebang行(
#!/usr/bin/env bash
或类似代码)?脚本看起来像什么?@shammelee在问题中编辑了
cgi gin/script.sh
?另外,请指定您的服务器/配置。@ShammelLee我在问题中添加了该脚本。我正在本地主机上运行它,HTTP服务正在运行。您是否在脚本顶部指定shebang行(
#!/usr/bin/env bash
或类似行)?