简体   繁体   中英

Ping selected IPs from checkbox on PHP-MySQL query

请查看图片了解更多详情

I have an idea and wonder if it can be or not on PHP since I'm new to PHP. I need to ping selected computer name or IP address and give me the status on ping status box as online or offline.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <title>Books</title> </head> <body> <form action="Untitled-1.php" method="post"> <input type="text" name="search"/> <input type="submit" value="search"/> <input type="button"value="ping"/> </form> </form> <br/> <table border="1"> <thead> <tr> <th>username</th> <th>desktop</th> <th>ip address</th> <th>select pc's</th> <th>ping status</th> </tr> </thead> <?php include("db.php"); $word = isset ($_POST['search']) ? $_POST['search'] : ""; $result=mysql_query("SELECT * FROM pc WHERE desktop like '%$word%'"); while($test = mysql_fetch_array($result)) { $id = $test['user_id']; ?> <tr align='center'> <td><font color='black'><?php echo $test['username'] ?></font></td> <td><font color='black'><?php echo $test['desktop'] ?> </font></td> <td><font color='black'><?php echo $test['ip_address'] ?></font></td> <td><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td> <td><font color='black'><?php echo $test['ping_status'] ?></font></td> </tr> <?php } ?> </table> </body> </html> 

please help

Pass ip in this function it give status of host.

 <?php

    function ping_host($host){
    exec("ping -c 4 " . $host, $output, $result);
     return $result==0?"online":"offline";

    }

    echo ping("www.google.com"); // function call

   ?>

Note:Here 4 is number of ping you want you can change according to requirement in linux system if you not set this it will ping forever.

thanks alot Sunil for try to help me , i found the selution and it's work fine with me now

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <?php require("db.php"); ?> <body> <form method="post"> <table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example"> <div class="alert alert-info"> <strong><i class="icon-user icon-large"></i></strong> </div> <thead> <tr> <th>username</th> <th>desktop</th> <th>ip address</th> <th>select</th> <th>status</th> </tr> </thead> <tbody> <?php $query=mysql_query("select * from pc")or die(mysql_error()); while($row=mysql_fetch_array($query)){ $id=$row['ip_address']; ?> <tr> <td><?php echo $row['username'] ?></td> <td><?php echo $row['desktop'] ?></td> <td><?php echo $row['ip_address'] ?></td> <td> <input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> </td> <td><?php echo $row['ping_status'] ?></td> </tr> <?php } if (isset($_POST['submit'])){ if(!empty($_POST['selector'])){ foreach($_POST['selector'] as $id){ if (!$socket = @fsockopen($id, 80, $errno, $errstr, 30)) { $status= "offline"; echo $status; } else { $status= "online"; echo $status; fclose($socket); } $sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'"; $query=mysql_query($sql); header("Location:test.php"); } } } ?> </tbody> </table> <button class="btn btn-success" name="submit" type="submit"> ping </button> </form> </body> </html> 

please find the last solution ,

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <?php require("db.php"); ?> <body> <form method="post"> <table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example"> <div class="alert alert-info"> <strong><i class="icon-user icon-large"></i></strong> </div> <thead> <tr> <th>username</th> <th>desktop</th> <th>ip address</th> <th>select</th> <th>status</th> </tr> </thead> <tbody> <?php $query=mysql_query("select * from pc")or die(mysql_error()); while($row=mysql_fetch_array($query)){ $id=$row['ip_address']; ?> <tr> <td><?php echo $row['username'] ?></td> <td><?php echo $row['desktop'] ?></td> <td><?php echo $row['ip_address'] ?></td> <td> <input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> </td> <td><?php echo $row['ping_status'] ?></td> </tr> <?php } $sql1="UPDATE pc SET ping_status = NULL WHERE ping_status is not null"; $query1=mysql_query($sql1); function pingAddress($id) { $pingresult = exec("ping -n 1 $id && exit", $output, $result); //echo $result. "<br/>"; global $status; if (($result == 0)){ if(count(preg_grep('/Destination host unreachable/i', $output)) == 0){ $status="online <br/>"; echo $status; }else $status="offline <br/>"; echo $status; } elseif ($result == 1){ $status="offline <br/>"; echo $status; } } if (isset($_POST['submit'])){ if(!empty($_POST['selector'])){ foreach($_POST['selector'] as $id){ echo $id.""; pingAddress($id); $sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'"; $query=mysql_query($sql); header("Location:test.php"); } } } ?> </tbody> </table> <button class="btn btn-success" name="submit" type="submit"> ping </button> </form> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM