plumdavid 发表于 2013-07-20 12:10

怎样把服务器上的ping命令运行过程在web页面中动态展示

本帖最后由 plumdavid 于 2013-07-20 12:10 编辑

想在PHP中调用服务器(linux系统)上的ping命令,把ping的返回过程在web页面中,有什么办法实现吗?

如:$ ping www.sina.com
PING cernetnews.sina.com.cn (121.194.0.239) 56(84) bytes of data.
64 bytes from 121.194.0.239: icmp_seq=1 ttl=59 time=0.391 ms
64 bytes from 121.194.0.239: icmp_seq=2 ttl=59 time=0.385 ms
64 bytes from 121.194.0.239: icmp_seq=3 ttl=59 time=0.459 ms
64 bytes from 121.194.0.239: icmp_seq=4 ttl=59 time=0.331 ms
64 bytes from 121.194.0.239: icmp_seq=5 ttl=59 time=0.600 ms
64 bytes from 121.194.0.239: icmp_seq=6 ttl=59 time=0.486 ms
64 bytes from 121.194.0.239: icmp_seq=7 ttl=59 time=0.491 msping命令返回记录是不断添加的,在web页面中也要达到这样效果




bikong0411 发表于 2013-07-20 15:44

可以考虑用libevent

plumdavid 发表于 2013-07-20 17:33

本帖最后由 plumdavid 于 2013-07-20 17:37 编辑

回复 2# bikong0411


    好高端,我看看先。

幽烛 发表于 2013-07-30 17:26

本帖最后由 幽烛 于 2013-07-30 17:29 编辑

function do_command($commandName, $args)
{
    $buffer = "";
    if (false === ($command = find_command($commandName))) return false;
    if ($fp = @popen("$command $args", 'r'))
        {
                while (!@feof($fp))
                {
                        $buffer .= @fgets($fp, 4096);
                }
                @pclose($fp);
                return trim($buffer);
        }
    return false;
}
function find_command($commandName)
{
    $path = array('/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
    foreach($path as $p)
    {
      if (@is_executable("$p/$commandName")) return "$p/$commandName";
    }
    return false;
}

plumdavid 发表于 2013-07-31 17:36

回复 4# 幽烛


    大侠,你这是查找文件?
    我想把ping命令的终端输出在web页面里展示,你的代码能实现吗?

Cyberman.Wu 发表于 2013-07-31 19:24

研究一下Ajax能否实现。普通后台脚本程序,一次执行就结束了。

plumdavid 发表于 2013-07-31 22:08

回复 6# Cyberman.Wu


    我用ajax实现了,但比较复杂,具体方法如下:

ping命令写入文件中,使用ajax调用文件数据在web页面中.
实现是实现了,但文本\linux命令\php语言之间的耦合度太高了,代码可维护性不高.

幽烛 发表于 2013-08-01 13:15

哎~~看来楼主还没看懂代码:dizzy:

回复 7# plumdavid


   

plumdavid 发表于 2013-08-01 13:48

回复 8# 幽烛


    长知识了.
    虽然你算法中用到的一些函数我不熟悉,但你的思路我明白了,多谢.

sohusina 发表于 2013-08-01 15:13

学习了,幽烛的代码有点像网页画图的做法。
页: [1] 2 3
查看完整版本: 怎样把服务器上的ping命令运行过程在web页面中动态展示