Execute script on remote host and obtain variables on localhost This executes a predefined script on a remote host, and somehow retrieve variable values without affecting STDOUT and STDERR.

My idea was to encode the data in JSON, write it to a file, then read the data in a separate request.

The cons of this method are that:


I would need to add the following code (file_put_contents) to the bottom of every script.
It requires a separate request (albeit, a pretty light request)


This is a dumbed down example, but inside the script to be run remotely:

&lt;?php
$data = array(...);
$json = json_encode($data);
file_put_contents('tmp.json', $json);


On the local side:

&lt;?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh-&gt;login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh-&gt;exec('php script.php');
$json = $ssh-&gt;exec('cat tmp.json');


I'm just looking for some input or alternative ways of doing this.
This has numerous problems with it, one of which is the race condition. Imagine two clients connect to the server at the same time. Which client's tmp.json wins? The other client gets bad data.

Another problem with the current way of doing things is that you start a new PHP interpreter for each request. So, it's not as lightweight as you're envisioning.

You really need to develop a protocol for sending the two data streams back, and then implement that protocol. A temporary file should not be necessary.