将 PHP 变量传递给远程 bash 脚本

Passing PHP variable to remote bash script

是否有类似于 PHP 的 bash 脚本的 GET 方法?

这就是我想要做的...

PHP 文件设置一个变量并执行远程 bash 脚本,该变量被传递并在 bash 脚本中使用。

所以这是我的 PHP 脚本...

 <?php
 $myvar = 'hello world';

 $connection = ssh2_connect('example.com', 22);
 ssh2_auth_password($connection, 'username', 'password');

 $stream = ssh2_exec($connection, '/usr/incoming/myscript.sh');
 ?>

这是远程机器上的 myscript.sh...

 #!/bin/bash          
 echo $myvar

当然这是行不通的,因为我不确定如何将 $myvar 从 PHP 文件传递​​到 myscript.sh

我知道 PHP 这是使用 GET 完成的,但在这种情况下我该怎么做?

正如@Phylogenesis 评论的那样,您想在脚本调用之后简单地放置变量,然后调用 bash 脚本使用 </code> 读取变量:</p> <p><a href="https://eval.in/439053" rel="nofollow">https://eval.in/439053</a></p> <pre><code>$stream = "/usr/incoming/myscript.sh " . escapeshellarg($myvar);

您的 bash 脚本如下所示:

#!/bin/bash          
 echo ""

script.php

 <?php
 $myvar = 'hello world';
 $myvar = escapeshellarg($myvar);
 $connection = ssh2_connect('example.com', 22);
 ssh2_auth_password($connection, 'username', 'password');
 $stream = ssh2_exec($connection, '/usr/incoming/myscript.sh '.$myvar);

myscript.sh

#!/bin/bash          
echo 

我最近发布了一个项目,允许 PHP 获取真实的 Bash shell 并与之交互,甚至可以通过 SSH。在这里获取:https://github.com/merlinthemagic/MTS

下载后您只需使用以下代码:

$shell = \MTS\Factories::getDevices()->getRemoteHost('example.com')->getShellBySsh('username', 'secretPassword');

//instead of triggering a script, just trigger the individual commands i.e.:
$return1  = $shell->exeCmd("echo $myvar");