php ping类(需开放危险函数exec)

2023-11-17 18:28:44 浏览数 (3)

忘记谁写的了,之前直接执行exec被吊毛把库删了,记得严格判断可以使用gethostbynamegethostbynamel函数解析成ipv4再传入调用!!切记!!!使用危险函数必须严格再严格判断后使用

代码语言:javascript复制
function ping_time($ip) {
        $ping_cmd = "ping -c 1 -w 5 " . $ip;
        exec($ping_cmd, $info);
        //判断是否丢包
        if(empty($info))return ['code'=> 400,'msg'=>"Ping请求找不到主机".$ip.",请检查该名称,然后重试"];
        $str1 = $info['4'];
        $str2 = "1 packets transmitted, 1 received, 0% packet loss";
        if( strpos( $str1 , $str2 ) === false) return ['code'=> 400,'msg'=>"当前网络堵塞,请求无法成功,请稍后重试"];
        $ping_time_line = end($info);
        $code = 200;
        $ping_time = explode("=", $ping_time_line)[1];
        $ping_time_min = explode("/", $ping_time)[0] ;
        $ping_time_avg = explode("/", $ping_time)[1] ;
        $ping_time_max = explode("/", $ping_time)[2] ;
        $ip = substr(explode(" ",$info[1])[3], 0, -1);

    return  ['code'=>$code,'msg'=>'ok','data'=>['domain_ip' => $ip,'ping_min'  => $ping_time_min.'ms','ping_avg'  => $ping_time_avg.'ms','ping_max' => $ping_time_max.'ms']];
}</code></pre>

0 人点赞