ssh接続からストリームの終わりを検出するのに問題があります。これは私が試したものです:
<pre><?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("localhost", 22)))
echo "fail: unable to establish connection\n";
else
if(!$ssh = ssh2_auth_password($con, "viassh", "sshpass")) {
echo "fail: unable to authenticate\n";
}
$sshStream = ssh2_shell($con, 'xterm', null, 80, 24, SSH2_TERM_UNIT_CHARS);
$strCommand = 'cd /var/www/test';
fwrite($sshStream, $strCommand . PHP_EOL);
stream_set_blocking($sshStream, true);
// infinite loop
#$stderr_stream = fgets(ssh2_fetch_stream($sshStream, SSH2_STREAM_STDERR), 8192);
#var_dump($stderr_stream);
// infinite loop
#var_dump(stream_get_contents($sshStream));
$ret = "";
while ($buf = fgets($sshStream)) {
$ret .= $buf;
// always false
#$meta = stream_get_meta_data($sshStream);
#var_dump($meta['eof']);
// works
if(strpos($buf, '$ cd ') !== false) break;
}
$strCommand = 'git status';
fwrite($sshStream, $strCommand . PHP_EOL);
stream_set_blocking($sshStream, true);
while ($buf = fgets($sshStream)) {
$ret .= $buf;
if (strpos($buf, '-a') !== false) break;
}
fclose($sshStream);
echo $ret;
?>
それは私に正しい出力を与えます:
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-32-generic-pae i686)
* Documentation: https://help.ubuntu.com/
20 packages can be updated.
0 updates are security updates.
Last login: Tue Oct 23 16:45:20 2012 from localhost
cd /var/www/test
]0;viassh@rcdev: ~viassh@rcdev:~$ cd /var/www/test
git status
]0;viassh@rcdev: /var/www/testviassh@rcdev:/var/www/test$ git status
# On branch develop
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: ssh_1.php
# modified: ssh_1.php~
# modified: ssh_shell.php
# modified: ssh_shell.php~
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# ssh_stream1.php
# ssh_stream1.php~
no changes added to commit (use "git add" and/or "git commit -a")
しかし、私がストリームの最後の行を知っているからです。feof()を試しましたが、失敗しました。だからうまくいけば、誰かが私を助けてくれるでしょう。