繁体   English   中英

从PHP-MySQL查询的复选框Ping选择的IP

[英]Ping selected IPs from checkbox on PHP-MySQL query

请查看图片了解更多详情

我有一个主意,想知道它是否可以在PHP上运行,因为我是PHP新手。 我需要对选定的计算机名称或IP地址执行ping操作,并在ping status框上将其状态设置为联机或脱机。

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

请帮忙

在此功能中传递ip即可获得主机状态。

 <?php

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

    }

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

   ?>

注意:这里4是您希望ping的数目,您可以根据需要在linux系统中进行更改,如果您未设置此值,它将永远ping通。

非常感谢Sunil尝试帮助我,我找到了解决方案,现在对我来说很好

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

请找到最后的解决方案,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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