简体   繁体   English

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

[英]Passing PHP variable to remote bash script

Is there a method similar to PHP's GET for bash scripts? 是否有类似于PHP的bash脚本GET的方法?

This is what I'm trying to do... 这就是我想要做的...

PHP file sets a variable and executes a remote bash script, the variable gets passed and is used in the bash script. PHP文件设置一个变量并执行一个远程bash脚本,该变量将被传递并在bash脚本中使用。

So this is my PHP script... 这是我的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');
 ?>

And this is myscript.sh on the remote machine... 这是远程计算机上的myscript.sh ...

 #!/bin/bash          
 echo $myvar

Of course this isn't working because I'm not sure how to pass $myvar from the PHP file to myscript.sh 当然,这是行不通的,因为我不确定如何将$ myvar从PHP文件传递到myscript.sh

I know with PHP this is done using GET but how do I do this in this situation? 我知道使用PHP可以使用GET完成,但是在这种情况下我该怎么办?

I recently published a project that allows PHP to obtain and interact with a real Bash shell, even through SSH. 我最近发布了一个项目,该项目允许PHP即使通过SSH也可以获取真实的Bash shell并与之交互。 Get it here: https://github.com/merlinthemagic/MTS 在这里获取它: https : //github.com/merlinthemagic/MTS

After downloading you would simply use the following code: 下载后,您只需使用以下代码:

$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");

As @Phylogenesis commented, you want to simply put the variables after the script call, then call in the bash script use $1 to read the var: 正如@Phylogenesis所说,您只想简单地将变量放在脚本调用之后,然后在bash脚本中调用使用$1读取var:

https://eval.in/439053 https://eval.in/439053

$stream = "/usr/incoming/myscript.sh " . escapeshellarg($myvar);

Your bash script would look like: 您的bash脚本如下所示:

#!/bin/bash          
 echo "$1"

script.php 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 myscript.sh

#!/bin/bash          
echo $1

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

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