2012-09-07 31 views
1

我有一個數據庫,我已創建,我用我的網絡上的靜態IP列表跟上。我有一個按順序列出ips的頁面,我的腳本列出了來自數據庫的ips和其他數據,但我想添加一個腳本來ping每個ip地址並告訴它是否啓動,下面是代碼I已經顯示一切:php主機狀態與ping與mysql查詢的結果

$dbConn = connectToDb(); 

$sql= <<< END 

SELECT * 
FROM `ipadds` 
ORDER BY oct1 ASC, oct2 ASC, oct3 ASC, oct4 ASC; 

END; 

$result=mysql_query($sql) or die(mysql_error()); 



$options=""; 



while ($row=mysql_fetch_array($result)) { 

    $oct1=$row['oct1']; 
    $oct2=$row['oct2']; 
    $oct3=$row['oct3']; 
    $oct4=$row['oct4']; 
    $SubnetMask=$row['SubnetMask']; 
    $Hostname=$row['Hostname']; 
    $MAC=$row['MAC']; 
    $Description=$row['Description']; 
    $DeviceType=$row['DeviceType']; 
    $Location=$row['Location']; 
    $Comments=$row['Comments']; 

    $options.="<tr><td><a href=editcomputer.php?queryID=$id><img src=images/edit.gif alt=print border=0></a> <a href=deletecomputer.php?queryID=$id><img src=images/edit-delete-icon.png alt=print border=0></a></td><td><a href='HTTP://$oct1.$oct2.$oct3.$oct4' target=_blank>$oct1.$oct2.$oct3.$oct4</a></td><td>$SubnetMask</td><td>$Hostname</td><td>$MAC</td><td>$Description</td><td>$DeviceType</td><td>$Location</td></tr>"; 

} 
?> 
<center> 

<font size=-1> 

<img src="images/edit.gif"> - Edit Computer<br> 

<img src="images/edit-delete-icon.png"> - Delete Computer<br> 

</font> 

<font size="2"> 

<table> 

<tr> 

<td> 

<hr> 

</td> 

</tr> 
<table cellpadding="15" border="1"> 
<tr> 
<td> 

</td> 
<td> 
<u>IP Address</u> 
</td> 
<td> 
<u>Subnet Mask</u> 
</td> 
<td> 
<u>Hostname</u> 
</td> 
<td> 
<u>MAC Address</u> 
</td> 
<td> 
<u>Description</u> 
</td> 
<td> 
<u>Device Type</u> 
</td> 
<td> 
<u>Location</u> 
</td> 

<?=$options?> 
</table> 

我想另一行添加到我的表,並把它作爲一個向上或向下的「狀態」,有什麼建議?

我發現這個代碼,它的工作全部由自己和我指定的地址,但不能讓它在我的「而」語句的工作:

function pingAddress($ip) { 
    $pingresult = exec("ping -n 3 $ip", $outcome, $status); 
    if (0 == $status) { 
     $status = "alive"; 
    } else { 
     $status = "dead"; 
    } 
    echo "The IP address, $ip, is ".$status; 
} 

pingAddress("ip addres of host here"); 
+0

提高您編碼風格的解決方案。當你有時間閱讀這個http://pear.php.net/manual/en/standards.php – Deepak

回答

1

沒有提的是,你不應該使用mysql_*功能(哎呀,我只是做了),你所要做的就是改變你的函數返回值:

function pingAddress($ip) { 
    $pingresult = exec("ping -n 3 $ip", $outcome, $status); 
    if (0 == $status) { 
     return true; 
    } 
    return false; 
} 

然後在循環:

while ($row=mysql_fetch_array($result)) { 
    [...] 
    if (pingAddress($ip) === true) { 
     // IP is up 
    } 
    else { 
     // IP is down 
    } 
} 
+1

工作很好,謝謝!對不起,如果我的編碼很糟糕,這對我來說更像是一份愛好,而不是一份工作。我從來沒有找到任何我喜歡的應用程序來做我需要做的事情,所以我非常瞭解了php和mysql的基礎知識,並且在本站和谷歌的幫助下,我寫了自己的。感謝您的幫助,這是我正在構建的一款出色的應用程序,至少對我來說這是:) –