In my case the ssh2_shell doesn't work every time. There were many problems:
- If I use stream_get_contents, the command(s) never executed
- with while ($buf = fgets...) the loop never stops
- without sleep(1) before stream_set_blocking, the command wasn't execute every time. Anyone knows why?
That's what I made, and it works - but I think it isn't what it should be:
<?php
$strCommand = 'tar -C /path/to/extract -xjf /path/from/file.tar.bz2';
$sshStream = ssh2_shell($this->sshSession, 'xterm', null, 120, 24, SSH2_TERM_UNIT_CHARS);
fwrite($sshStream, $strCommand . PHP_EOL);
sleep(1);
stream_set_blocking($sshStream, true);
$sshUntarRetval = "";
while ($buf = fgets($sshStream,4096)) {
flush();
$sshUntarRetval .= $buf;
if (strpos($buf, 'tar -C') !== false)
{
break;
}
}
fclose($sshStream);
?>