开发者

Format exec() system() output

开发者 https://www.devze.com 2023-04-13 10:05 出处:网络
I need to the get the output of a command on unix on a webpage, in a tabular format. This is what I used to do :

I need to the get the output of a command on unix on a webpage, in a tabular format. This is what I used to do : This basically generates a html code, which i redirect to a html page and access it from a webpage.

echo "<html lang=en xml:lang=en xmlns=http://www.w3.org/1999/xhtml>"
echo "<head>"
echo "<title> Team Page </title>"
echo "</head>"
echo "<body bgcolor=#ddfedg font-family=Comic Sans MS>"
echo "<table border=1>"
cat $i|grep %|grep -v on|awk '{printf "<tr><td>%-10s</td> <td>%-10s</td> 
</tr>\n",$NF,$(NF-1)}'
echo "</table>
</body>
</html>"

Previously , I used to set it as a cronjob on unix and generate a html page for every 15 minutes.

Now, I want to make this access realtime..like fresh data is loaded each time the page is loaded. I tried using the exec() and system() functions in php ,but I am not able to figure out how to tabulate the output.

The output shows up as :

Array ( 
  [0] => / 5% 
  [1] => /stand 10% 
  [2] => /var 36%
  [3] => /usr 40%
  [4] => /ts_undo 31%
  [5] => /ts_temp 96%
  [6] => /ts_redo3 13%
  [7] => /ts_redo2 13%
  [8] => /ts_redo1 13%
  [9] => /ts_index 7%
  [10] => /ts_data 96%
  [11] => /tmp 54%
  [12] => /test_db 65% 
  [13] => /oracle 22%
  [14] => /oraarch 36%
  [15] => /opt 20%开发者_Go百科
  [16] => /home 38%
  [17] => /Oracle10g 76%
) 

Please help me out on this issue.


// Print out the HTML head
echo "<html lang=\"en\" xml:lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">

  <head>
    <title> Team Page </title>
  </head>

  <body bgcolor=\"#ddfedg\" font-family=\"Comic Sans MS\">
    <table border=\"1\">\n";

// Execute the command to get the data
$cmd = 'bdf|grep %|grep -v on|awk \'{printf "%-10s %-10s\n",$NF,$(NF-1)}\'';
exec($cmd,$output);

// Loop the data
foreach ($output as $line) {
  // Split the row into mount name and usage value
  list($mount,$usage) = preg_split("/\s+/", $line, PREG_SPLIT_NO_EMPTY);
  // Print a table row
  echo "          <tr>\n            <td>$mount</td>\n            <td>$usage</td>\n          </tr>\n";
}

/*

 Assuming the output of 'bdf' is the same as 'df' you could just parse
 the entire output using PHP, and get more information, like this:

 exec('df',$output);
 array_shift($output); // Get rid of column headers
 foreach ($output as $line) { // Loop the remaining data
   if (count($line = preg_split("/\s+/", $line, PREG_SPLIT_NO_EMPTY)) <= 1) continue; // Skip emtpy lines
   echo "          <tr>\n"; // Start an new row
   foreach ($line as $col) echo "            <td>$col</td>\n"; // Print all the columns
   echo "          </tr>\n"; // End the row
 }

*/

// Print the end of the HTML
echo "        </table>\n      </body>\n\n    </html>";

As you can see, it's not that dissimilar to the bash approach, we just loop the data to turn it into a table.

0

精彩评论

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