I am trying to pull a list of ipaddresses ping them then update the ping status to a seperate database. Everything works except the updating piece. Any help would be appreciated .. Here is my code.
#!/usr/bin/php -q
<?php
$dbserver = "";
$dbname = "";
$dbprefix = "";
$dbusername = "";
$dbpassword = "";
$OS = "2";
$yes = yes;
// Connect
$link = mysql_connect($dbserver, $dbusername, $dbpassword)
OR die(mysql_error());
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
die ('Can\'t use $dbname : ' . mysql_error());
}
function quote_smart($value)
{
if (!get_magic_quotes_gpc()) {
$value = addslashes($value);
}
if (!is_numeric($value)) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
$address_array = array();
$query = sprintf("SELECT ipadd, username FROM ips WHERE ips.monitor = 'yes'");
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
$string = $row['username'] . ":" . $row['ipadd'];
array_push($address_array, $string);
}
function ping($pc,$ip,$OS){
if ($OS == "1") {
$cmd = shell_exec("ping -n 1 $ip");
} elseif ($OS == "2") {
$cmd = shell_exec("ping -c 1 $ip");
}
$data_mount = explode(",",$cmd);
if (eregi("0", $data_mount[1])) {
$connection = "offline";
}
if (eregi("1", $data_mount[1])) {
$connection = "online";
}
$result = "<tr><td align='center'>$connection</td><td align='center'><i>$pc</i></td><td align='center'><a href=snmp.php?ip=$ip target=_blank>$ip</a></td></tr>";
return $result;
$con = mysql_connect($dbserver, $dbusername, $dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
mysql_query("UPDATE ipstatus SET status = '$connection' WHERE ipadd = '$ip'");
}
echo "<title>Community</title>
<table cellspacing='0' width='40%'>
<tr>
<td width='15%' align='center'><b>Status</b></td>
<td width='45%' align='center'><b>Customer Name</b></td>
<td width='30%' align='center'><b>IP Address</b></td>
</tr>";
while(list($k,$v) = each($address_array)){
$ip = explode(":",$v);
$result = ping($ip[0],$ip[1],$OS);
echo $result;
}
echo "</table><br><br>";
echo $power;
?>