how to ping ip addresses in php and give results [

2019-01-22 13:49发布

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>

?>

3条回答
Juvenile、少年°
2楼-- · 2019-01-22 14:30

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),
查看更多
迷人小祖宗
3楼-- · 2019-01-22 14:41
$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
)
查看更多
我只想做你的唯一
4楼-- · 2019-01-22 14:44

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"

查看更多
登录 后发表回答