Jump to content

PHP writing to a file from an output stream only writes the first few lines


kkmoslehpour

Recommended Posts

I am trying to write to a file (file.txt) from an output stream from a remote server. The output stream's content is generated from a bash/expect script on a remote server. I am not sure how `fwrite()` works, however, when I echo the `fget()` I can see the whole output from the script. 

 

Does anyone know if `fwrite()` only writes the first few lines of the output stream? Or when does it stop writing? I have tried using a while loop to get it to continuously to write but I only see the first few lines in my file.txt ` while($line = fgets($stream_out)) {fwrite($fopenText, $line);}` . However, when I echo the `fget()` I can see the whole output stream on the webpage I need the full output to be written to my file.txt, which is not doing.

 

How can I accomplish this? My guess is something with the buffer stream??

 



    $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 output:

 

    [root@psat main]# cat file.txt


[root@psat main]# cat file.txt
    cd /home/user/
    My current directory is:/home/user
    ./myExpectScript.sh

 

expected file.txt output (what i get back from `$_POST['option'] == 2` echo):

 

    cd /home/user/


  cd /home/user/
    My current directory is:/home/user/
    ./myExpectScript.sh
    spawn /bin/bash
    [user@gateway user]$ spawn /bin/bash
    [user@gateway user]$ ./hexMsg -o 3 20000 0 0 0
    ??
    [user@gateway user]$ /usr/local/bin/ssh " @ "
    ...
    ...
    (omitted for simplicity)

 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.