脚本检测联网状态
2016-08-09 本文已影响0人
hel2o
脚本检测联网状态
<?php
/**
* 发送post请求
* @param string $url 请求地址
* @param array $post_data post键值对数据
* @return string
*/
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
//读取2个字符串之间的字符,读取成功返回读取字符,失败返回0
class get_c_str {
var $str;
var $start_str;
var $end_str;
var $start_pos;
var $end_pos;
var $c_str_l;
var $contents;
function get_str($str,$start_str,$end_str){
$this->str = $str;
$this->start_str = $start_str;
$this->end_str = $end_str;
$this->start_pos = strpos($this->str,$this->start_str)+strlen($this->start_str);
$this->end_pos = strpos($this->str,$this->end_str);
$this->c_str_l = $this->end_pos - $this->start_pos;
$this->contents = substr($this->str,$this->start_pos,$this->c_str_l);
return $this->contents;
}
}
//检测字符串中是否包含特定字符
function checkstr($str,$needle){
$tmparray = explode($needle,$str);
if(count($tmparray)>1){return true;} else{return false;}
}
//PING检测是否通,通返回数据 result:1 time=延时;不通返回 result:0 time=timeout 时间为ms
function pingcheck($ip){
exec("ping $ip -c 1 -W 1",$info);
$get_c_str = new get_c_str;
if(checkstr($info[4],"1 received")){
$ping = 1;
$time = $get_c_str -> get_str($info[4],'time ','ms');
}else{
$ping = 0;
$time = "timeout";
}
return array('result' => $ping , 'time' => $time);
}
//检测iptable
function iptablecheck(){
exec("/sbin/iptables -L",$fuck);
if(checkstr($fuck[2],"REJECT")){$iptable=1;}else{$iptable=0;}
return $iptable;
}
//调用pingcheck后返回ping的结果,返回ping成功的次数
function check($ip, $times, $interval){
for ($i=1; $i <= $times ; $i++) {
$do = pingcheck($ip);
echo "ping".$i."=".$do['result']." time".$i."=".$do['time']."ms\n";
$result = $do['result'] + $result;
sleep($interval);
}
return $result;
}
$t1 = microtime(true);
$iptable = iptablecheck();
$ip = '114.114.114.114';
$times = 10;
$all = check($ip, $times, 4);
if( $all >= 4 ){
$ping = 1;//通
}else{
$ping = 0;//不通
}
echo "\nresult: ping通次数=".$all;
if ($ping==0 && $iptable==0){
exec("/sbin/iptables -I INPUT 1 -s 192.168.99.17/30 -m state --state NEW -m tcp -p tcp -m multiport --dports 80,81,88,100,1100,2525,8099 -j REJECT");
$do = "插入iptables";
echo "\n".$do."\n";
$post_data = array(
'host' => '代理(电信)',
'times' => $times,
'errors' => $times-$all,
'do' => $do,
'status' => "iptable:".iptablecheck()."--ping:".$ping
);
send_post('http://192.168.99.16:8000', $post_data);
}
elseif($ping ==1 && $iptable==1){
exec("/sbin/iptables -D INPUT 1");
$do = "删除iptables";
echo "\n".$do."\n";
$post_data = array(
'host' => '代理(电信)',
'times' => $times,
'errors' => $times-$all,
'do' => $do,
'status' => "iptable:".iptablecheck()."--ping:".$ping
);
send_post('http://192.168.99.15:8000', $post_data);
}
echo "\niptable:".$iptable."--ping:".$ping."--时间:".date('y-m-d H:i:s',time())."\n";
$t2 = microtime(true);
echo '耗时'.round($t2-$t1,3)."秒\n";
?>