Using PHP to view syslog in private web page

I am trying to extract data from syslog and view it on a web page. I'm using a php script.

This line works from command line

tail -n 11 /var/log/_gateway-syslog.log | grep -a iWiSP-Out | awk '{print $8, $9,$10,$11"   ",$19,"   " $15, $16,$17,"   " $18,$21, $22}'

when I put it into this php script it fails isp.php

<?php
$output = shell_exec('tail -n 11 /var/log/_gateway-syslog.log | grep -a iWiSP-Out | awk '{print $8, $9,$10,$11"   ",$19,"   " $15, $16,$17,"   " $18,$21, $22}' ');
echo "<pre>$output</pre>";
?>

web page test.html




<html>
<body>
<iframe width=1450 height=200  allowtransparency="true" style="background: #FFFFFF;"  src="isp.php" style="border: 5px solid black;">
  <p>Your browser does not support iframes.</p>
</iframe>
</body>
</html>



You need to watch your quotes and escaping. In PHP this is all one big double-quoted section, and any double quotes inside it must be escaped so they don't end early.

<?php
$output = shell_exec("tail -n 11 /var/log/_gateway-syslog.log | grep -a iWiSP-Out | awk '{print $8, $9,$10,$11\"   \",$19,\"   \" $15, $16,$17,\"   \" $18,$21, $22}' ");
echo "<pre>$output</pre>";
?>
1 Like

Generally in PHP you are better off with a simple shell_exec() command and then process the results in PHP.

For example, from the above code, you could do this:

<?php
$command = "tail -n 11 /var/log/_gateway-syslog.log");
$raw_output = shell_exec($command);
$output = process_output($raw_output):
echo "<pre>$output</pre>";

function  process_output($text)
{
  // do your text processing here instead of using a shell command pipeline in shell_exec()
 return $processed_text;
}

PHP has rich tools for text processing including regular expressions.

I written these types of PHP files to display unix / linux system information countless times, and I always process the text in PHP without exception.

In general, I find it best to decouple the "shell exec" type of commands from PHP as much as possible for a number of reasons, including system security, integrity and long-term system maintenance.

For a real-world example on this web site, we use shell_exec() to begin processing all the man pages in our man page repository; but the shell_exec() only returns the raw output from the man command. Then the PHP script extensively processes the raw man page output, adding links, formatting, html entities, css information, etc. There is a significant amount of text processing done in PHP, but the PHP shell_exec() command to get the man page from the Linux filesystem and output for processing is very basic / simple.

Corona688, Thank you. That was the quick fix and it worked....

Neo,

Thank you, I like your idea and I have followed the logic in your suggestion. However I am lost when it gets to processed_output. everything I have tried gives me no output.

I even replaced the tail -n with just cat to make sure I would see something. under the function process_output I ran a simple test using a grep for 20.254 . I'm seeing nothing.

You must write your own text processing function and post YOUR code.

Where is the the code YOU wrote that you are taking about?