Reputation: 37
I have a database that I have created that I use to keep up with my list of static IPs on my network. I have a page that lists the ips in order, my script to list the ips and other data from the database works, but I want to add a script to ping each ip address and tell whether it's up or not, here is the code I have to display everything:
$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>
I want to add another row to my table and have it as a "Status" of Up or Down, any suggestions?
I found this code, it works all by itself and I specify the address, but can't get it to work in my "while" statement:
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");
Upvotes: 1
Views: 2753
Reputation: 24383
Without mentioning that you should not be using mysql_*
functions (oops, I just did), all you have to do is change your function to return a value:
function pingAddress($ip) {
$pingresult = exec("ping -n 3 $ip", $outcome, $status);
if (0 == $status) {
return true;
}
return false;
}
then in your loop:
while ($row=mysql_fetch_array($result)) {
[...]
if (pingAddress($ip) === true) {
// IP is up
}
else {
// IP is down
}
}
Upvotes: 1