簡體   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