格式化 exec() system() 输出

发布于 2024-12-10 12:02:37 字数 1205 浏览 0 评论 0原文

我需要在网页上以表格格式获取 unix 上命令的输出。 这就是我曾经做过的: 这基本上会生成一个 html 代码,我将其重定向到 html 页面并从网页访问它。

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>"

以前,我曾经将其设置为unix上的cronjob,并每15分钟生成一个html页面。

现在,我想让这种访问实时进行。就像每次加载页面时都会加载新数据一样。 我尝试在 php 中使用 exec() 和 system() 函数,但我无法弄清楚如何对输出进行制表。

输出显示为:

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%
  [16] => /home 38%
  [17] => /Oracle10g 76%
) 

请帮助我解决这个问题。

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%
  [16] => /home 38%
  [17] => /Oracle10g 76%
) 

Please help me out on this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

焚却相思 2024-12-17 12:02:37
// 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>";

正如您所看到的,它与 bash 方法没有什么不同,我们只是循环数据以将其转换为表。

// 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文