开发者

problem formatting php output

开发者 https://www.devze.com 2023-04-07 17:06 出处:网络
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 t

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);


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)


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>';


Try using htmlspecialchars() to escape anything that will cause rendering problems in HTML:

print '<pre>' . htmlspecialchars($output) . '</pre>';

The pre tag will respect whitespace and defaults to a monospaced font so your lines will look like they would in a console.


Not tested but I guess it will work since php doc says

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.

ob_start();
system("ssh user@testServer ls -al");

$output = ob_get_clean();

echo '<pre>';
echo $output; 
echo '</pre>';
0

精彩评论

暂无评论...
验证码 换一张
取 消