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:

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

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

$data = array(...);
$json = json_encode($data);
file_put_contents('tmp.json', $json);

On the local side:


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

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

I'm just looking for some input or alternative ways of doing this.

  • \$\begingroup\$ Welcome to CodeReview, Devon. Unfortunately CodeReview is not the site to ask for implementation suggestions, or obtain advice on unwritten code. In addition, snippets and stub code are Off-Topic. CodeReview is about improving completed, fully functional, working code. When your code works and does exactly what you want it to, come back and we'll help you do the same thing in a cleaner way! \$\endgroup\$
    – Legato
    Commented Apr 6, 2015 at 23:51
  • 1
    \$\begingroup\$ Legato, the above code does work if I insert data into the $data variable and add my SSH login info. I just excluded some sensitive information and information unrelated to the question. \$\endgroup\$
    – Devon
    Commented Apr 7, 2015 at 0:02
  • \$\begingroup\$ So, both the client and the server are running PHP scripts -- but not in a web browser/server? What problem are you trying to solve here? Why is this method the only way to solve the problem? \$\endgroup\$
    – Snowbody
    Commented Apr 7, 2015 at 7:50
  • \$\begingroup\$ @Snowbody, it's a variety of system management scripts. The output is provided to the end user, but the scripts could also have other variables that are needed for processing on the local server. The local server could be run from the browser, it doesn't really make a difference on that. I'm sure this isn't the only method, hence why I asked for input or alternatives. The remote server could potentially run any language, but the variables still need a way to be extracted, PHP is just a common binary on all the servers so it makes sense to use it. \$\endgroup\$
    – Devon
    Commented Apr 7, 2015 at 14:46
  • \$\begingroup\$ So the local side connects to the server and causes a script to be run, but not by acting as an http client/server (the typical use of PHP), but instead SSL client/server for authentication. The script's output gets echoed back to the client, and the additional data comes in via a separate stream (bu the same connection). \$\endgroup\$
    – Snowbody
    Commented Apr 7, 2015 at 17:00

1 Answer 1


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.

  • \$\begingroup\$ Yes, tmp.json would most likely end up being a unique identifier. As for the second stream of data, that is why I was trying to figure out. Through SSH, I'm not sure how I would get a second stream of data other than STDOUT and STDERR. \$\endgroup\$
    – Devon
    Commented Apr 7, 2015 at 18:04
  • \$\begingroup\$ new php interpreter for each request. So it's not as lightweight as you're envisioning. I'm also not sure what you mean by this. Are you referring to the execution of the remote script? In that case, that would be the case for any non-compiled language. \$\endgroup\$
    – Devon
    Commented Apr 7, 2015 at 18:23
  • \$\begingroup\$ yes...unless you set things up so that there was only one php process, and something else triggers the load of the script (similar to how things work with php integrated into a web server) \$\endgroup\$
    – Snowbody
    Commented Apr 7, 2015 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.