我是php的新手,我有这个问题....
<?php
$RANGE = 192.168.1.1/254;
for in $RANGE
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if ! ping -c $COUNT $myhost; then
# 100% failed
fi
fi
done
echo "Host : $myHost is down (ping failed) at $(date)" | mail -s "$SUBJECT" $EMAILID
这个想法是:
提前预订
答案 0 :(得分:2)
您可以使用fsockopen函数检查主机的主机和端口
$hosts = array(/* array of hosts list */) foreach ($hosts as $host) { $hostname = $host; $port = 80; $timeout = 3; $fp = fsockopen ($hostname, $port ,$errno ,$errstr, $timeout); if($fp) { // Port is alive // Mount, send an email, insert to db } else { // Port is dead. Reason : $errstr } }
答案 1 :(得分:2)
$mainpart = "192.168.1.";
$errors = array();
foreach(range(1, 254) as $ip) {
$adr = $main . $ip;
$msg = exec("ping {$adr} blablabla");
if($msg == "bad error") {
$errors[$adr] = $msg;
}
}
每个循环可能需要某种超时。最后,您可以遍历errors-array来处理给定的每个错误。如果可能的话,我会考虑使用fsockopen检查端口而不是像Osaman推荐的ping命令。