Jump to content

First Post: Output remote server stream bash file with line breaks on page PHP


kkmoslehpour

Recommended Posts

Hi all,

 

I am attempting to output a remote bash file onto a page so the user can see what is happening. I am able to read the bash file, however, anything I try it isn't adding a newline between each echo statements. Does anyone know how to do this?

 

Here is my bash file:

 

    #!/bin/bash


  #!/bin/bash


    echo 'touch /tmp/testfile'."\n"
    echo "I am up\n"
    echo '\n'
    echo "\n"
    echo
    echo 'hello there'

 

Here is my php logic:

 



    if(isset($_POST['option']) && $_POST['option'] == 1) { 
                    $stream = ssh2_exec($connection, "/tmp/user/testscripts/up.sh");
                    stream_set_blocking($stream, true);
                    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
                    echo stream_get_contents($stream_out);


               }


 

Here is my output on the page:

 

touch /tmp/testfile.\n I am in up\n \n \n hello there

Link to comment
Share on other sites

Am I reading this correctly in that you 'bash file' is the file you are using the ssh calls to retrieve?

 

In that case why does the bash file have all that php code in it?  It would seem that the bash file is simply data and your ssh2 calls are simply supposed to retrieve it. so the echo means nothing.

 

How about trying the example that is in the manual and do a loop that uses fgets to read the stream?

Link to comment
Share on other sites

Am I reading this correctly in that you 'bash file' is the file you are using the ssh calls to retrieve?

 

In that case why does the bash file have all that php code in it?  It would seem that the bash file is simply data and your ssh2 calls are simply supposed to retrieve it. so the echo means nothing.

 

How about trying the example that is in the manual and do a loop that uses fgets to read the stream?

Hi ginerjm,

 

No sorry, to clarify, I have a bash file in a remote server, let's say, script.sh. I have another php file called, main.php that is trying to output the contents of the bash file on to the webpage. Two separate files here. I am able to read the echo's from the remote script.sh, however, the spacing and linebreaks are off. 

 

Thanks

Link to comment
Share on other sites

It's a shell script. Not sure what's so confusing about that.

 

OP: What do you need the newline sequences for? The Bash echo already terminates each string with a newline. However, PHP scripts by default output HTML, and an ASCII newline is not an HTML newline. Either change the content type of the HTTP response to text/plain or use a <pre> element or convert the newlines to <br> elements.

Link to comment
Share on other sites

I found the answer, when you are converting from bash to html you can use the following:

 

                    echo '<pre>' . stream_get_contents($stream_out) . '</pre>';
 
                                                   OR
 
                    echo nl2br(stream_get_contents($stream_out));
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.