リモートサーバの出力ストリームからファイル(file.txt)に書き込もうとしています。出力ストリームのコンテンツは、リモートサーバー上のbash/expectスクリプトから生成されます。 fget()
をエコーすると、スクリプトからの出力全体が表示されますが、どのようにfwrite()
の動作がわかりません。PHPが出力ストリームからファイルに書き込むのは、最初の数行だけを書きます。
fwrite()
が出力ストリームの最初の数行しか書き込んでいない場合は誰にも分かりますか?それとも、いつ書くのをやめますか?私はwhileループを使用して書き込みを継続しようとしましたが、私はfile.txt while($line = fgets($stream_out)) {fwrite($fopenText, $line);}
の最初の数行しか表示しません。しかし、私がfget()
をエコーすると、ウェブページ上の出力ストリーム全体が見えますが、私はfile.txtに完全な出力を書き込む必要があります。
どうすればこの問題を解決できますか?
$gwUser = 'user';
$gwPwd = 'pwd';
$pathToScript1 = '/home/user/up.sh';
$pathToScript2 = '/home/user/down.sh';
if ($connection = @ssh2_connect($gateway, 22)) {
ssh2_auth_password($connection, $gwUser, $gwPwd);
if(isset($_POST['option']) && $_POST['option'] == 1) {
$stream = ssh2_exec($connection, $pathToScript1);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
$path = $_SERVER['DOCUMENT_ROOT'] . "/my/path/file.txt";
touch($path);
chmod($path, 0777);
$fopenText = fopen($path, "w");
while($line = fgets($stream_out)) {fwrite($fopenText, $line);} //this doesn't give me the full output stream
echo '<pre>' . "------------------------\n" . '</pre>';
while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
fclose($stream);
fclose($fopenText);
}
//I can get the the output stream this way, but this is not what i want.
if(isset($_POST['option']) && $_POST['option'] == 2) {
$stream = ssh2_exec($connection, "$pathToScript2");
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$stream_err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
while($line = fgets($stream_out)) {flush(); echo '<pre>' . $line . '</pre>';}
echo '<pre>' . "------------------------\n" . '</pre>';
while($line = fgets($stream_err)) {flush(); echo '<pre>' . $line . '</pre>';}
fclose($stream);
}
}
file.txtを出力:
[[email protected] main]# cat file.txt
cd /home/user/
My current directory is:/home/user
./myExpectScript.sh
期待file.txtを出力(私はバック$_POST['option'] == 2
エコーから何を得る):
cd /home/user/
My current directory is:/home/user/
./myExpectScript.sh
spawn /bin/bash
[[email protected] user]$ spawn /bin/bash
[[email protected] user]$ ./hexMsg -o 3 20000 0 0 0
??
[[email protected] user]$ /usr/local/bin/ssh " @ "
...
...
(omitted for simplicity)
' $ _POST [ 'オプション'] ==:各書き込みの後にバッファをフラッシュしてみてください'彼らは異なるシェルスクリプトを実行しています。出力が同じになるはずですか? – neuromatter
@neuromatter混乱して申し訳ありませんが、出力は問題ではありません。私はこの部分を動作させました。同じスクリプトを 'fwrite'と呼んでも、最初の数行しか表示されません。 2が私に必要なものをエコーします。私は完全にfile.txtに書き込むストリームを取得しようとしていますが、これは問題が発生している場所です。両方のスクリプトが同じことを今行っているとしましょう。 – kkmoslehpour
'-----'行が見えますか?もしそうでなければ、SSH接続は終了していない可能性があり、 'fwrite()'ループは終了していません。 – Barmar