Jump to content

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


kkmoslehpour
Go to solution Solved by Jacques1,

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

  • Solution

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.

  • Like 1
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

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.