php ping类(需开放危险函数exec)
忘记谁写的了,之前直接执行exec被吊毛把库删了,记得严格判断可以使用gethostbyname
或gethostbynamel
函数解析成ipv4再传入调用!!切记!!!使用危险函数必须严格再严格判断后使用
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>
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 七云's Blog!