从网页运行 bash 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17765755/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:58:44  来源:igfitidea点击:

Run bash file from web page

linuxbash

提问by Fuzzybear

Ok so I have a bash file that does various operations on a linux server.

好的,所以我有一个 bash 文件,可以在 linux 服务器上执行各种操作。

I need to pass 7-8 variables to the bash file, and these variables will be set by a user on a simple webpage.

我需要将 7-8 个变量传递给 bash 文件,这些变量将由用户在一个简单的网页上设置。

Just wondering if this is possible or do I need to script the bash file call through some other language?

只是想知道这是否可行,还是我需要通过其他语言编写 bash 文件调用的脚本?

Looking into calling functions on a bash file from http://www.tldp.org/LDP/abs/html/functions.htmlis this the right lines?

http://www.tldp.org/LDP/abs/html/functions.html 查看bash 文件上的调用函数,这是正确的行吗?

can't seem to find a good resource to guide me on this so any links or examples appreciated

似乎无法找到一个很好的资源来指导我,所以任何链接或示例都值得赞赏

回答by mti2935

If you're running PHP on the web server, then you can post your web form to a PHP script, then use PHP's shell_exec()function to call your bash script and pass the inputs. As paulsm4 mentions above, any time you call a shell script and pass inputs to a shell script from the web, you need to be very careful to sanitize the inputs to prevent injection type of attacks. One way to do this is to do all the validation and sanitization in the PHP script, before calling the shell script by way of shell_exec() to ensure that you are passing only clean input to the shell script.

如果您在 Web 服务器上运行 PHP,那么您可以将 Web 表单发布到 PHP 脚本,然后使用 PHP 的shell_exec()函数调用您的 bash 脚本并传递输入。正如 paulsm4 上面提到的,任何时候你调用一个 shell 脚本并将输入从 web 传递给一个 shell 脚本,你需要非常小心地清理输入以防止注入类型的攻击。一种方法是在通过 shell_exec() 调用 shell 脚本之前在 PHP 脚本中进行所有验证和清理,以确保您只将干净的输入传递给 shell 脚本。

See http://php.net/manual/en/function.shell-exec.php

http://php.net/manual/en/function.shell-exec.php

回答by paulsm4

Yes, you can. You want "CGI" (Common Gateway Interface). It's a huge potential security risk: be careful!

是的你可以。您需要“CGI”(通用网关接口)。这是一个巨大的潜在安全风险:小心!

Just Google for CGI bash. For example:

只是谷歌的 CGI bash。例如: