可能重复:
使用PHP ping IP地址并输出结果
如何在PHP中ping一个IP地址,并将结果显示为在Windows的cmd程序中一样。
<?php
system(‘ping -c 192.168.0.104’); // Ping IP address.<br>
echo “pinged”;<br>
?>
可能重复:
使用PHP ping IP地址并输出结果
如何在PHP中ping一个IP地址,并将结果显示为在Windows的cmd程序中一样。
<?php
system(‘ping -c 192.168.0.104’); // Ping IP address.<br>
echo “pinged”;<br>
?>
试试这个
$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!";
注意:这取决于您使用的操作系统。Windows 默认仅 ping 4 次,而 Linux 会一直 ping。
若要在 Windows 中 ping 两次,请使用“ping -n 2 主机”。
若要在 Linux 中 ping 两次,请使用“ping -c 2 主机”。
$ip = "127.0.0.1";
exec("ping -n 3 $ip", $output, $status);
print_r($output);
输出结果如下所示
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
)
Array()
,请尝试使用 -c 3
而不是 -n 3
。你可能正在运行 Linux 服务器而不是 Windows。Linux ping 不识别 -n
选项。 - Paloescapeshellcmd
来确保用户无法执行任意命令。 - Laxmana我刚刚使用了exec函数来ping谷歌。
<?php
echo exec("ping www.google.com");
?>
输出结果为:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),