20

Possible Duplicate:
Pinging an IP address using PHP and echoing the result

How do you ping an ip addresses in php. and give the the results as if you are on cmd program in windows

<?php

  system(‘ping -c 192.168.0.104’); // Ping IP address.<br>

   echo “pinged”;<br>

?>
Community
  • 1
  • 1
Mervyn
  • 861
  • 4
  • 15
  • 28
  • You get answer from this please check http://stackoverflow.com/questions/8030789/pinging-an-ip-address-using-php-and-echoing-the-result – Elby Nov 08 '12 at 06:29

3 Answers3

25

Try this

$host="192.168.0.104";

exec("ping -c 4 " . $host, $output, $result);

print_r($output);

if ($result == 0)

echo "Ping successful!";

else

echo "Ping unsuccessful!";

Note: This is dependant on the OS you are running. Windows will default to only 4 pings while Linux will ping forever.

To ping twice in Windows, use "ping -n 2 host"

To ping twice in Linux, use "ping -c 2 host"

Xethron
  • 1,116
  • 9
  • 24
Hkachhia
  • 4,463
  • 6
  • 41
  • 76
22
$ip =   "127.0.0.1";
exec("ping -n 3 $ip", $output, $status);
print_r($output);

output looks like below

Array
(
    [0] => 
    [1] => Pinging 127.0.0.1 with 32 bytes of data:
    [2] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    [3] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    [4] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    [5] => 
    [6] => Ping statistics for 127.0.0.1:
    [7] =>     Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
    [8] => Approximate round trip times in milli-seconds:
    [9] =>     Minimum = 0ms, Maximum = 0ms, Average = 0ms
)
arun
  • 3,667
  • 3
  • 29
  • 54
2

I just pinged google with that exec

<?php
echo exec("ping www.google.com");
?>

output was:

Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69