PHP Ping Ubuntu

PHP Ping Ubuntu

问题描述:

I am having trouble getting PHP to ping a local computer. I just want to be able to check if the computer is awake or not.

I have seen numerous methods to ping while searching, but none seem to work for me. I believe this is because on Ubuntu, ping requires root privileges, which PHP does not have. Most examples use some form of

exec("ping ".$ip);

but any method would be okay with me. Any ideas?

我无法让PHP ping本地计算机。 我只是想能够检查计算机是否处于清醒状态。 p>

我在搜索时遇到过很多ping方法,但似乎没有一种方法适合我。 我相信这是因为在Ubuntu上,ping需要root权限,PHP没有。 大多数示例使用某种形式的 p>

  exec(“ping”。$ ip); 
  code>  pre> 
 
 

但是任何方法 对我没问题。 有什么想法吗? p> div>

On my Ubuntu system, the command ls -l /bin/ping shows:

-rwsr-xr-x 1 root root 34716 2010-11-15 09:08 /bin/ping

This means that although "ping" is owned by root, all users can execute it.
You should try with the following PHP code:

<html>
<body>
<pre>
<?php 
$result = exec("/bin/ping -c 1 -W 1 127.0.0.1"); 
echo "result=".$result."
";
?>
</pre>
</body>
</html>

Obviuously, you should replace "127.0.0.1" with a valid IP address for your network.
The -c 1 option stops pinging after sending one packet, as described in the manual page:

-c count
Stop after sending count ECHO_REQUEST packets.
With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.

The -W 1 option forces ping to stop if it does not get a response after one second:

-W timeout
Time to wait for a response, in seconds.
The option affects only timeout in absense of any responses, otherwise ping waits for two RTTs.