duangu1033 2012-08-22 18:59
浏览 31
已采纳

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?

  • 写回答

1条回答 默认 最新

  • douzi7219 2012-08-22 20:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题