Hi,
If you are having problems with running a ssh2 session and it waits forever during the execution of stream_get_contents, it might be because the remote system has run the command and is now sitting at a # prompt waiting for the next command. I had this issue on a HP MSA box, here is the code to get around the issue.
Assuming you are connected with your authentication method and $ssh contains the handle.
<?php
$command = "check disk";
$stream = ssh2_shell ($ssh, 'xterm', null, 200, 200, SSH2_TERM_UNIT_CHARS);
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking ($stream, true);
stream_set_blocking($errorStream, true);
fwrite ($stream, $command . PHP_EOL );
sleep(2);
fwrite ($stream, "exit" . PHP_EOL );
sleep (2);
echo stream_get_contents($stream);
$errortext=stream_get_contents($errorStream);
if (strlen($errortext) > 0) {
echo "Error Data: $errortext";
exit (1);
}
exit (0);
?>
You can't use ssh2_exec with this method (well at lease I couldn't) because on executing the first command the stream gets blocked and then you can't run the exit command, whereas a terminal seems to use one session.
I hope this helps someone.