php 如何在php中ping ip地址并给出结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13283674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to ping ip addresses in php and give results
提问by Mervyn
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.ini 中 ping 一个 IP 地址?并给出结果,就像你在 windows 中的 cmd 程序一样
<?php
system(‘ping -c 192.168.0.104'); // Ping IP address.<br>
echo “pinged”;<br>
?>
回答by Hkachhia
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.
注意:这取决于您运行的操作系统。Windows 将默认为只有 4 个 ping,而 Linux 将永远 ping。
To ping twice in Windows, use "ping -n 2 host"
要在 Windows 中 ping 两次,请使用“ping -n 2 host”
To ping twice in Linux, use "ping -c 2 host"
要在 Linux 中 ping 两次,请使用“ping -c 2 host”
回答by arun
$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
)

