3

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);
2
  • 1
    Show some example output please. Commented Sep 23, 2011 at 15:34
  • Is PHP running as a server module. In that case see my answer, it addresses the problem. Commented Sep 23, 2011 at 15:49

4 Answers 4

7

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)

Sign up to request clarification or add additional context in comments.

1 Comment

This does not work, as system() automatically flushes the PHP output after each line from shell is recieved. To properly format the output, you must do echo '<pre>' before the call to system(), as I have done in my answer
1

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

1 Comment

Alternatively, using exec() like exec('ssh user@testServer ls -al', $output); would give you an array of the output, without having to produce spaghetti code
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.