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.

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

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源