Hello,
i have this little script, which should ping the IPs in the $hosts_to_ping
array. This PHP is called with JS in the index.html.
But something is wrong, because the $rval
is always 1 (which mean the host is unreachable). But i know that the first two host is alive.
So I print the $res
variable, and i see the message: Need to give the IP. I dont understand why dont replace the $host
variable to the actual IP address in the function.
<?php
function ping($host) {
exec(sprintf('ping -n 4', escapeshellarg($host)), $res, $rval);
print_r($res);
return $rval === 0;
}
$hosts_to_ping = array('10.54.23.254', '10.22.23.254', '10.23.66.134');
?>
<ul>
<?php foreach ($hosts_to_ping as $host): ?>
<li>
<?php echo $host; ?>
<?php $up = ping($host); ?>
(<img src="<?php echo $up ? 'on' : 'off'; ?>"
alt="<?php echo $up ? 'up' : 'down'; ?>">)
</li>
<?php endforeach; ?>
</ul>