- 论坛徽章:
- 0
|
另外一种ping port 方式的
#!perl
use Net::Ping;
use Color::Output;
Color::Output::Init;
$|=1;
my $target=shift||'127.0.0.1';#目标主机
my $prt=shift || '80'; #应用协议
my $host="";
my $duration="";
my $ip="";
$count1=0;
$count2=0;
$count3=0;
$count4=0;
$stime=time;
$lstime=localtime;
$alertcount1=0;
$alertcount2=0;
ti();
while(sleep(4))
{
until (-e "SpeedTest-$target.log")
{
open(TLOG,"+>>SpeedTest-$target.log");
print TLOG <<END;
N e t w o r k S p e e d T e s t
www.xtiger.net Beta Ver0.1
____________________________________________________________
异常状况统计报告:
END
close TLOG;
}
$p = Net::Ping->new("syn");
$p->{port_num} = $prt;
$p->hires();
$p->ping($target);
if(($host,$duration,$ip) = $p->ack and $duration!=0)
{
system("cls");
ti();
printf("\n 与目标主机:$host [ip:$ip] $prt服务\n 保持通讯中 (连接速度: %.2f ms)\n", $duration*1000);
if($duration<0.05)
{
$count1++;
system("title 网络良好.保持通讯中.目标主机:$host.应用服务:$prt");
}
if($duration<0.1 and $duration>=0.05)
{
$count2++;
system("title 网络一般.保持通讯中.目标主机:$host.应用服务:$prt");
}
if($duration>=0.1)
{
$count3++;
system("title 网络较差.保持通讯中.目标主机:$host.应用服务:$prt");
$alertcount1++;
if($alertcount1%5==0)
{
open(ALOG,"+>>SpeedTest-$target.log");
print ALOG "网络异常.连接失败.目标主机:$target.应用服务:$prt 。从", scalar($lstime),"起这样的情况出现了$alertcount1次!\n\n";
close ALOG;
}
}
}
else
{
system("cls");
ti();
print "\n 连接到目标主机:$target $prt服务失败!\n";
system("title 网络异常.连接失败.目标主机:$target.应用服务:$prt");
$count4++;
$alertcount2++;
if($alertcount2%5==0)
{
open(ALOG,"+>>SpeedTest-$target.log");
print ALOG "网络异常.连接失败.目标主机:$target.应用服务:$prt 。从", scalar($lstime),"起这样的情况出现了$alertcount2次!\n\n";
close ALOG;
}
}
$p->close();
$ct=$count1+$count2+$count3+$count4;
$bf1=$count1/$ct;
$bf2=$count2/$ct;
$bf3=$count3/$ct;
$bf4=$count4/$ct;
$bf11=int($bf1*100);
$bf21=int($bf2*100);
$bf31=int($bf3*100);
$bf41=int($bf4*100);
$str1=">"x(int($bf1*20));
$str2=">"x(int($bf2*20));
$str3=">"x(int($bf3*20));
$str4=">"x(int($bf4*20));
$str11=" "x(20-int($bf1*20));
$str21=" "x(20-int($bf2*20));
$str31=" "x(20-int($bf3*20));
$str41=" "x(20-int($bf4*20));
$etime=time;
$ttime=$etime-$stime;
($hour,$minute,$second)=stime($ttime);
cprin("—"x30,7);
cprin("\n 统 计: $hour小时$minute分$second秒 进行$ct次连接测试",9);
cprin("\n 良 好 率:[>$str1$str11]$bf11% ($count1/$ct)",7);
cprin("\n 一 般 率:[>$str2$str21]$bf21% ($count2/$ct)",11);
cprin("\n 较 差 率:[>$str3$str31]$bf31% ($count3/$ct)",13);
cprin("\n 异 常 率:[>$str4$str41]$bf41% ($count4/$ct)",5);
}
sub ti
{
print <<END;
N e t w o r k S p e e d T e s t
www.xtiger.net Beta Ver0.1
____________________________________________________________
END
}
sub cprin
{
($str,$i)=@_;
cprint("\x03" . $i . "$str\n\x030");
}
sub stime
{
my $stime=shift;
my $hour=int($stime/(60*60));
my $minute=int(($stime-($hour*60*60))/60);
my $second=$stime-$hour*60*60-$minute*60;
return ($hour,$minute,$second);
} |
|