kkmoslehpour kkmoslehpour -3 years ago 120
PHP Question

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

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:



echo 'touch /tmp/testfile'."\n"
echo "I am up\n"
echo '\n'
echo "\n"
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);
$output = stream_get_contents($stream_out);


<div class="box1">
<form method="post">
<label class="col">Up/Down</label>
<span class="col">
<input type="radio" name="option" id="r1" value="1" />
<label for="r1">Up</label>
<input type="radio" name="option" id="r2" value="2" />
<label for="r2">Down</label>
<span class="col">
<input type="submit" class="button"/>

echo "<pre>$output</pre>"

Here is my output on the page:

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

Answer Source

bash echo appends a newline unless suppressed with -n, so don't include them. There is a -e option to have echo interpret escaped characters but you don't need that here.

Also, to display on a web page (HTML) you will need to wrap in <pre></pre> tags or use the PHP nl2br().

So use:

echo 'touch /tmp/testfile'
echo 'I am up'
echo 'hello there'


echo 'touch /tmp/testfile
I am up

hello there'

HTML doesn't render newlines so this treats it as pre-formatted text that will:

echo '<pre>' . stream_get_contents($stream_out) . '</pre>';

Or convert the newlines to the HTML <br> tags:

echo nl2br(stream_get_contents($stream_out));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download