Ping in PHP + Debian Apache2

Asked

Viewed 141 times

0

Good morning,

I managed to search the internet make the script below work as what I needed, but still lack a detail,ai I have to consequently drip into a machine,as I could do to be a list of machines,without I need to repeat the commands for each machine ?

My idea would even be to make this integration with mysq database,that would then give a select bringing the registered ip’s there.

Follows the code:

<html>
<head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <META HTTP-EQUIV="REFRESH" CONTENT="5">

        <title>PAINEL DE MONITORAMENTO</title>

        <link rel="stylesheet" href="bootstrap/simplex/style.css">
        <link rel="stylesheet" href="bootstrap/simplex/bootstrap.min.css">
        <link rel="stylesheet" href="bootstrap/css/datatables.css">
        <script src="bootstrap/js/jquery.min.js"></script>
        <script src="bootstrap/js/bootstrap.min.js"></script>
        <script src="bootstrap/js/jquery.dataTables.min.js"></script>
        <script src="bootstrap/js/datatables.js"></script>
</head>
<body>
<div class='container'>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">PAINEL DE MONITORAMENTO   |</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
          </ul>
        </li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"></a></li>
      </ul>
    </div>
  </div>
</nav>
<div><a href="#"></a><img src="on.png" alt="ATIVO">  ATIVO
     <a href="#"></a><img src="off.png" alt="INATIVO">  INATIVO</div>
 <br>
 <?php
function ping($host)
{
        exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
        return $rval === 0;
}

$host = '192.168.0.77';
$up = ping($host);
?>
<div class"container">
    <div class="row">
     <div class="col-md-4"> 
        <?php
         echo"<div class='list-group'>";
           echo"<a href='#' class='list-group-item active'>IP: $host </a>";
           echo"<a href='#' class='list-group-item'>NOME:</a>"; 
           echo'<a href="#"  class="list-group-item">STATUS: <img src="'.($up ? 'on' : 'off').'.png" alt="'.($up ? 'up' : 'down').'" /></a>';
         echo"</div>";
         echo "<br/>";
         ?>
     </div>
     <div class="col-md-4"> 
        <?php
         echo"<div class='list-group'>";
           echo"<a href='#' class='list-group-item active'>IP: $host </a>";
           echo"<a href='#' class='list-group-item'>NOME:</a>"; 
           echo'<a href="#"  class="list-group-item">STATUS: <img src="'.($up ? 'on' : 'off').'.png" alt="'.($up ? 'up' : 'down').'" /></a>';
         echo"</div>";
         echo "<br/>";
         ?>
     </div>
     <div class="col-md-4">
        <?php
         echo"<div class='list-group'>";
           echo"<a href='#' class='list-group-item active'>IP: $host </a>";
           echo"<a href='#' class='list-group-item'>NOME:</a>"; 
           echo'<a href="#"  class="list-group-item">STATUS: <img src="'.($up ? 'on' : 'off').'.png" alt="'.($up ? 'up' : 'down').'" /></a>';
         echo"</div>";
         echo "<br/>";
         ?>
     </div>
   </div>
  <br>
</body>
</html>

@rray : I did by the while but this bringing only the first result,:

<?php
require_once('conexao.php'); 
  $result = mysql_query("SELECT srv_ip AS IP, srv_nome AS NOME_SERVIDOR FROM servidores");
   if (!$result) {
    echo 'Não foi possível executar a consulta: ' . mysql_error();
    exit;
   }
    while ($row = mysql_fetch_array($result)) {
      $srv_ip    = $row["IP"];
      $srv_name  = $row["NOME_SERVIDOR"];


function ping($host)
{
        exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
        return $rval === 0;
}

$host = $srv_ip;
$up = ping($host);

echo "<div class'container'>";
     echo "<div class='row'>";
       echo "<div class='col-md-4'> ";
         echo"<div class='list-group'>";
           echo"<a href='#' class='list-group-item active'>IP: $host </a>";
           echo"<a href='#' class='list-group-item'>NOME:$host</a>"; 
           echo'<a href="#"  class="list-group-item">STATUS: <img src="'.($up ? 'on' : 'off').'.png" alt="'.($up ? 'up' : 'down').'" /></a>';
          echo"</div>";
         echo "<br/>";
        echo "</div>";
         }
     ?>
  • Uses an array with a loop(while, for or foreach).

  • @rray could please see the supplement I put to the question.

  • The call of the function ping() and that div container also go into the while.

  • About the container I removed from php,more about Function I didn’t understand well,I’m calling it wrong ? because I need it because it does the ping.

  • while ($row = mysql_fetch_array($result)) { ping($row['IP']); //...demais linhas

  • 1

    100% Thanks. I made the change and its right.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.