Using PING with PHP

3

I have a function that needs to ping 5 machines on the internal network. I am using the command below:

 //EXECUTA O PING
 exec("ping " . $host, $output, $result);

What happens is as follows. If I put a non-standard ip, such as "192.999.999.999" the function test, below, correctly returns that does not exist (OFF):

// TESTA O RESULTADO
if ($result == 0) {
  echo "ON";
} else {
  echo "OFF";
}

Now, if I put a correct ip, but that is only offline, it always returns me ON. If the ip is even online it also returns me ON, which is correct.

Is there any other way to use this ping in PHP? So that it recognizes when ip is offline?

    
asked by anonymous 13.10.2016 / 13:28

2 answers

2

I edited the command exec , adding the following:

exec("ping -n 1 -w 1 " . $host, $output, $result);

In this way, in addition to gaining time, the result became correct.

    
13.10.2016 / 14:28
2

Try this:

exec('ping 127.0.0.1', $saida, $retorno);
if (count($saida)) {
    print 'A Máquina está online e os dados do PING foram gravados em $saida. :)';
} else {
    print 'A Máquina NÃO está online ou o host não pode ser encontrado. :(';
}
    
13.10.2016 / 13:50