# 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:

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:

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


On the local side:

<?php
include('Net/SSH2.php');

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

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. • 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! – Legato Apr 6 '15 at 23:51 • 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. – Devon Apr 7 '15 at 0:02
• 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? – Snowbody Apr 7 '15 at 7:50
• @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. – Devon Apr 7 '15 at 14:46
• 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). – Snowbody Apr 7 '15 at 17:00

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.
• 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. – Devon Apr 7 '15 at 18:23