簡體   English   中英

從HTML執行Shell腳本

[英]Execute a shell script from HTML

我編寫了一個簡單的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>"

您可以使用服務器端語言。 使用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保存文件

這是您可以在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>

不同文件擴展名的處理程序(添加.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

檢查是否在您的環境中按預期配置了所有內容。

順便說一句,好的做法:不要給腳本提供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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM