// Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothingecho "Output: " . stream_get_contents($stream);echo "Error: " . stream_get_contents($errorStream);

// Close the streams fclose($errorStream);fclose($stream);

?>

My initial suspicion is that either a) I've done something wrong or b) the blocking nature of both streams means that by the time first stream has received data and returned, the buffer for the second stream has already emptied.

I've done preliminary tests with blocking disabled, but haven't had any luck there either.

I believe most of the problem that people are having here is that there is a misconception about what blocking *really* means.

Blocking means a read from the *stream* will wait until there is data. Not necessarily all the data from the application -- but *some*. So it won't help you at all if you're executing a command that doesn't write to stdout, or writes a whole lot of data.

So there are 2 problems:

1. If you need to know that a silent program is done via ssh2_exec, you'll need to signal it to yourself. ssh2_exec will *not* block execution until the command is done executing. And 2 consecutive ssh2_execs may execute asynchronously. You could also log into a shell via ssh2_shell and parse up to the next prompt -- but that's overkill. You can also do this by adding on some sort of sentinel at the end of your command, such as echo "@," and then block on reads until you see a "@." Ensure "@" won't appear in the output, or escape the output via some encoding mechanism if you can't do that.

2. If the program takes awhile, you have the same problem. You need to read until you're done. So you need a sentinel value like the above.

3. Sleep() is just a bad idea here. Commands rarely take the same amount of time to execute a command twice. It may be OK if you're doing *one* thing and can just wait 5 seconds. But that's not cool if it's something you're doing in a loop.

If you want to run many exec orders throught ssh2 in the same process using the same variable $stream for exemple and to read the output for each order, you must close the stream after each order else you will not be able to read the next order output.

This is the best way I found to automatically run multiple commands or commands that might take longer then expected. NOTE: this assumes that no where in the output is there the text '[end]' otherwise the function will end prematurely. Hope this helps people.

//Trick is in the start and end echos which can be executed in both *nix and windows systems.
//Do add 'cmd /C' to the start of $cmd if on a windows system.
$cmd = "echo '[start]';your commands here;echo '[end]'";
$output = user_exec($shell,$cmd);

Nowhere in the manual does it imply that stream_get_contents is required for the actual execution to take place, but in this case.. it seems that way. So, if you're having trouble getting commands to execute, try using stream_get_contents().