#!/usr/bin/perl#use strict;#use warnings;use LWP::UserAgent;use HTTP::Cookies;use Encode;use JSON;use utf8;binmode(STDIN, ':encoding(utf8)');binmode(STDOUT, ':encoding(utf8)');binmode(STDERR, ':encoding(utf8)');my $new_task_url = 'http://www.alibench.com/new_task.php'; #创建探测人物的URLmy $get_task_re_url = 'http://www.alibench.com/query_task.php'; # 获取探测结果URLsub save_cookie { ####保存COOKIE 并获取form name(下边创建任务post时会用到) my $UA = LWP::UserAgent->new; my $cookie_jar = HTTP::Cookies->new( file => "./ali_cookies.dat", autosave => 1, ignore_discard => 1, ); $UA->cookie_jar($cookie_jar); ##保存COOKIE my $response = $UA->get('http://www.alibench.com/'); my ($traceroute_from,$traceroute_from_name); my $str = $response->content; foreach my $line (split(/input/,$str)){ if ($line =~ m!.*name="traceroute_from" value="([^"]*)" id.*!) { $traceroute_from = $1; #获取form } if ($line =~ m!.*name="traceroute_from_name" value="([^"]*)" id.*!) { $traceroute_from_name = $1; #获取form name } } return ($traceroute_from,$traceroute_from_name);}sub create_task { my ($traceroute_from,$traceroute_from_name) = @_; my $UA = LWP::UserAgent->new; $UA->agent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0'); $UA->cookie_jar({'file' => './ali_cookies.dat'}); $UA->default_header( #定义headers 'Host' => 'www.alibench.com', 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0', 'Accept' => 'application/json, text/javascript, */*; q=0.01', 'Accept-Language' => 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3', 'Accept-Encoding' => 'gzip, deflate', 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With' => 'XMLHttpRequest', 'Referer' => 'http://www.alibench.com/', 'Connection' => 'keep-alive', 'Pragma' => 'no-cache', 'Cache-Contro' => 'no-cache', ); my $response = $UA->post("$new_task_url",['task_from' => 'self', ##post数据 'traceroute_from' => "$traceroute_from", 'traceroute_from_name' => "$traceroute_from_name", 'target' => 'www.test.com', ##定义需要探测的URL 'target2' => '', 'is_pk' => 'false', ##对比开关 'ac' => 'http', ##测试类型为http 'http_assign_time' => '20', ##任务下发时间 'isps[]' => '1', ##需要探测的服务商 'isps[]' => '4', 'http_method' => 'get', 'http_gzip' => 'true', 'http_follow_302' => 'true', 'http_cookie' => '', 'http_ua' => '', 'http_host' => '', 'http_referer' => '', 'http_limit_rate' => '', ]); return $response->content; ##包含任务id 以及创建任务的返回码 ,0为成功}sub get_task_re { ##获取探测结果 my ($task_id) = @_; my $UA = LWP::UserAgent->new; $UA->agent('Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0'); $UA->cookie_jar({'file' => './ali_cookies.dat'}); $UA->default_header( 'Host' => 'www.alibench.com', 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0', 'Accept' => 'application/json, text/javascript, */*; q=0.01', 'Accept-Language' => 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3', 'Accept-Encoding' => 'gzip, deflate', 'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With' => 'XMLHttpRequest', 'Referer' => "http://www.alibench.com/rp/$task_id", 'Connection' => 'keep-alive', 'Pragma' => 'no-cache', 'Cache-Contro' => 'no-cache', ); my $response = $UA->post("$get_task_re_url",[ 'task_ids' => "$task_id", 'task_type'=> '1', ]); return $response->content;}sub handle_json { ###json处理 my $json_data = $_[0]; my $json_obj; my $json = JSON->new->utf8; $json_obj = $json->decode("$json_data"); my ($data_array,$tmp,$complete_status); my @data_item = qw!http_total_time http_dns_time curl_connect_time http_download_speed client_ip node_name!; ###定义需要获取的数据类型,以下是可以得到的信息,在数组data_item添加即可# {# "node_id": "10658334",# "locate_id": 34147844,# "task_type": "1",# "target_ip": "123.134.186.225",# "http_response_code": "200",# "http_total_time": 296000,# "http_dns_time": 15000,# "curl_connect_time": 62000,# "curl_pretransfer_time": 62000,# "curl_starttransfer_time": 140000,# "http_download_size": "36590",# "http_download_speed": "123614",# "http_response_head": "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLast-Modified: Wed, 04 Feb 2015 08:55:16 GMT\r\nVary: Accept-Encoding\r\nExpires: Wed, 04 Feb 2015 09:08:23 GMT\r\nCache-Control: max-age=300\r\nContent-Encoding: gzip\r\nContent-Length: 36590\r\nAccept-Ranges: bytes\r\nDate: Wed, 04 Feb 2015 09:05:33 \r\nServer: wxcdn/1.1.1\r\n\r\n",# "ldns_ip": "58.242.96.242",# "http_download_time": 234000,# "http_connect_time": 47000,# "status": 2,# "client_ip": "58.243.174.211",# "node_name": "安徽 阜阳 联通",# "node_country": "中国",# "node_province": "安徽",# "node_city": "阜阳",# "isp": "联通",# "target_location": "山东省莱芜市联通"# } for my $item (@{$json_obj->{'data'}}){ $data_array = $item->{'data'}; $complete_status = $item->{'complete'}; #true:1 false:0 } foreach my $one_zone_info (@{$data_array}) { if (defined($one_zone_info->{'http_total_time'})) { foreach my $da_it (@data_item) { $tmp .= $one_zone_info->{"$da_it"} . "_"; } push (@result,$tmp); #将探测完成的数据push到数组 $tmp = ''; }else{ print "$one_zone_info->{'node_name'} this zone is not ok \n"; next; } } return($complete_status,@result);}my ($traceroute_from,$traceroute_from_name) = save_cookie;my $task_info = create_task($traceroute_from,$traceroute_from_name);my $task_id;if ($task_info =~ m!\{"code":0,"env":"online","data":"\\/rp\\/(.*)"\}!) { $task_id = $1;}else{ print "Create task failed \n $task_info"; exit;}sleep 20;print "get \n";my ($complete_status,@result);($complete_status,@result) = handle_json(get_task_re($task_id));while ($complete_status == 0) { sleep 2; print "get result,waiting \n"; ($complete_status,@result) = handle_json(get_task_re($task_id));}foreach (@result) { print "$_ \n"; ##输出全部的数据}
下图是获取的部分结果。 注意下执行频率,过快的话 可能会导致测试的域名被拉黑名单。