Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem formatting php output

Tags:

php

I have a php website that I want to use to gather information from a unix server. A good example would be a script that would ssh into a server and run an ls command. I am having problems formatting the output so it is readable. Any help would be appreciated. The code would look something like this:

$output = system("ssh user@testServer ls -al");
print ($output);
like image 917
Justin Gunderson Avatar asked Mar 01 '26 12:03

Justin Gunderson


2 Answers

you probably want to use

echo "<pre>";
echo system("ssh user@testServer ls -al");
echo "</pre>";

which shows code in $output as-is (3 spaces shows as 3 spaces, a new line shows as a new line)

like image 189
genesis Avatar answered Mar 03 '26 01:03

genesis


The problem is this

The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.

So you need to do this like this:

echo '<pre>';
$output = system("ssh user@testServer ls -al");
echo '</pre>';

Alternative
As suggested by Deebster, if you have exec function enabled on your server you can do this like this also

$output = null;
exec("ssh user@testServer ls -al", $output);
echo '<pre>';
foreach($output as $line)
    echo $line . "\n";
echo '</pre>';
like image 26
danishgoel Avatar answered Mar 03 '26 00:03

danishgoel