Grant Grant - 6 months ago 35
PHP Question

Passing PHP variable to remote bash script

Is there a method similar to PHP's GET for bash scripts?

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.

So this is my PHP script...

<?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...

#!/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

I know with PHP this is done using GET but how do I do this in this situation?

Answer

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:

https://eval.in/439053

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

Your bash script would look like:

#!/bin/bash          
 echo "$1"