Jump to content

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


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)

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.