简体   繁体   English

从 HTML 超链接运行 Bash Shell 脚本?

[英]Run a Bash Shell Script from HTML Hyperlink?

I'm a Java Developer who is trying to learn HTML in painful baby steps.我是一名 Java 开发人员,正在努力学习 HTML 的痛苦婴儿步骤。 I'd like my nacent website to run a simple bash shell script (or better yet, a PHP script) when the user clicks a button or hyperlink.我希望我的新网站在用户单击按钮或超链接时运行一个简单的 bash shell 脚本(或者更好的是 PHP 脚本)。 It is surprisingly hard to find a simple example of this!很难找到一个简单的例子!

I found this example on StackOverflow.我在 StackOverflow 上找到了这个例子 The post's second answer lays out a step-by-step example, where the HTML code calls a PHP script;该帖子的第二个答案列出了一个分步示例,其中 HTML 代码调用 PHP 脚本; the PHP script calls a bash shell script. PHP 脚本调用 bash shell 脚本。 I've tried to replicate the example on my own web server.我试图在我自己的 web 服务器上复制该示例。 (FYI, I'm developing on an Ubuntu machine running Apache2.) (仅供参考,我正在运行 Apache2 的 Ubuntu 机器上开发。)

Here's my index.html :这是我的index.html

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <form action="/testexec.php">
        <input type="submit" value="Run My Script">
    </form>
  </body>
</html>

Keeping it simple, this creates a button to call testexec.php , which is in the Apache home directory ( /var/www/html/ ) There's not much to that script, either:保持简单,这会创建一个按钮来调用testexec.php ,它位于 Apache 主目录( /var/www/html/ )该脚本也没有太多内容:

<?php
shell_exec("/var/www/html/myBashScript.sh");
header('Location: http://10.10.10.10/myBashScript.sh?success=true');
?>

I'm not sure about those file pathnames.我不确定这些文件路径名。 Strictly speaking, myBashScript.sh is in directory /var/www/html/ (although I suppose I could move it later).严格来说, myBashScript.sh位于目录/var/www/html/中(尽管我想我可以稍后移动它)。 When I troubleshoot, I can't tell if the pathnames are screwing up anything.当我进行故障排除时,我无法判断路径名是否搞砸了。 Also, I really hate that I have to refer to the server by its IP Address (here, 10.10.10.10 .) There's gotta be a way to just say localhost , right?另外,我真的很讨厌我必须通过它的 IP 地址(这里是10.10.10.10 )来引用服务器。总有一种方法可以说localhost ,对吧?

Here's myBashScript.sh:这是 myBashScript.sh:

#!/bin/bash
echo "HELLO WORLD"

...and that should do it. ...应该这样做。 So I thought.所以我认为。 Both scripts are set properly with chmod and chown and chgrp , and I can manually run them without errors from the command line.这两个脚本都使用chmodchownchgrp正确设置,我可以手动运行它们而不会从命令行出错。

But when I surf to my webpage and click "Run My Script", I see this in my web browser:但是当我浏览我的网页并单击“运行我的脚本”时,我在 web 浏览器中看到了这个:

#!/bin/bash
echo "HELLO WORLD"

In other words, the text of the script is printed to the screen.换句话说,脚本的文本被打印到屏幕上。 But I want the script to run and the output of the script to appear on screen.但我希望脚本运行并且脚本的 output 出现在屏幕上。 Gah, FWIW, I see no errors entered into the Apache2 error log ( /var/log/apache2/error.log ), either. Gah,FWIW,我也没有看到 Apache2 错误日志( /var/log/apache2/error.log )中输入任何错误。

Soooooo... anyone spot my error?太好了……有人发现我的错误吗? Thank you in advance.先感谢您。

I think that could enable CGI scripts in your webserver, send the script to /cgi-bin/ folder, rename it to myscript.cgi (remember to put the appropiate Shebang ), set 0755 permissions, correct owner/group and put in the very beginning of the script:我认为这可以在您的网络服务器中启用 CGI 脚本,将脚本发送到 /cgi-bin/ 文件夹,将其重命名为 myscript.cgi (记得放适当的 Shebang ),设置 0755 权限,正确的所有者/组并放入非常脚本的开头:

#!/bin/bash
echo "Content-Type: text/html"
... more code... 

The first echo line tells browser that the output of CGI script must be rendered as HTML (you could modify MIME type to your specific needs).第一个 echo 行告诉浏览器 CGI 脚本的 output 必须呈现为 HTML(您可以根据您的特定需要修改 MIME 类型)。 There's a functional example:有一个功能示例:

#!/bin/bash
echo -e "Content-type: text/html\r\n\r\n"
echo "<html>"
echo "<head>"
echo "<title>TEST</title>"
echo "</head>"
echo "<body>"
echo "<h1>It Works!, Bash script running as CGI</h1>"
echo "</body>"
echo "</html>"

I really hate that I have to refer to the server by its IP Address (here, 10.10.10.10.) There's gotta be a way to just say localhost, right?我真的很讨厌我必须通过它的 IP 地址(这里是 10.10.10.10)来引用服务器。总有一种方法可以说 localhost,对吧?

To reply to the question above, you can add the entry in /etc/hosts file in linux server.要回答上述问题,您可以在 linux 服务器的 /etc/hosts 文件中添加条目。 for eg, 10.10.10.10 localhost localhost.localdomain例如,10.10.10.10 localhost localhost.localdomain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM