开发者

php ping a range ip address

开发者 https://www.devze.com 2023-02-17 17:14 出处:网络
well i\'m new of php and i have this problem.... <?php $RANGE = 192.168.1.1/254; forin $RANGE do count=$(ping -c $COU开发者_JAVA百科NT $myHost | grep \'received\' | awk -F\',\' \'{ print $2 }\' |

well i'm new of php and i have this problem....

<?php
$RANGE = 192.168.1.1/254;

for  in $RANGE
do
count=$(ping -c $COU开发者_JAVA百科NT $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

the idea is :

  • 1: ping the Range
  • 2: mount an eventually server (if is alive)
  • 3: send me an email
  • 4: put all host alive in db

anyone can help me?!

tnx in advance


You can check hosts and ports of hosts with fsockopen function

$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
    }
}


Guess you could try something like this. Be warned though, I have not tested this code.

$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;
    }
}

Might need some sort of timeout for each loop. At the end you can loop through the errors-array to handle each error given. If possible I would consider using fsockopen to check on ports instead of pingcommands like Osaman recommends.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号