简体   繁体   English

PHP执行SH文件

[英]php execute sh file

I'm attempting to launch a sh file through PHP with arguments, however I cannot get this working at all: 我试图通过带参数的PHP启动sh文件,但是我根本无法正常工作:

<?php
$ip = $_GET['ip'];
$port = $_GET['port'];

echo shell_exec('sh var/www/html/Grant73565/Grant.sh $ip $port')or die("bash didn't work");
echo('Sent!');
?>

Running the file through ssh manually works fine like: 通过ssh手动运行文件可以正常工作,例如:

./Grant.sh 127.0.0.1 80 ./Grant.sh 127.0.0.1 80

However in php it just echo's "Bash didn't work". 但是在php中,它只是回显“ Bash无效”。

It's not to-do with the arguments as far as I know as it's not even launching the file without them. 据我所知,这与参数无关,因为没有参数就不会启动文件。

You need to use double-quotes if you want to include a variable. 如果要包含变量,则需要使用双引号。

echo shell_exec("sh var/www/html/Grant73565/Grant.sh $ip $port") or die("bash didn't work");

With your current code, anybody can alter the http query and execute anything on your server. 使用您当前的代码,任何人都可以更改http查询并在您的服务器上执行任何操作。 This is a major security hole. 这是一个主要的安全漏洞。

The solution is to verify the input. 解决方案是验证输入。 The port will always be numeric so that's simple. 端口将始终为数字,因此很简单。 You can use a regular expression to verify the IP address. 您可以使用正则表达式来验证IP地址。

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

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