Using PING with PHP

Asked

Viewed 6,624 times

5

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

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

What happens is this. If I put any ip out of the standard, like "192.999.999.999" the function test, below, returns correctly 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?

  • 1

    Strange he return ON since technically the ping could only return an IP that was connected to the network, I will do some tests here and return with something soon.

  • When you "drip" on that computer offline, which is the value of the variable result? see: var_dump($result);. Another thing, your OS is Windows?

  • Yes, it’s Windows. Actually already solved the problem, add the answer.

2 answers

4


I edited the command exec, adding the following:

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

That way, in addition to buying time, the result became correct.

2

Try it this way:

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. :(';
}
  • No, the machine is offline and it appears as on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.