免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3176 | 回复: 4

Network-Speed -Test.pl [复制链接]

论坛徽章:
0
发表于 2009-02-27 14:04 |显示全部楼层
又是一个自己用的东西,很早前写的。
有个朋友刚好想用到某个地方,所以贴到这里。

#!perl -w

use Net::Ping;
use Color::Output;
Color::Output::Init;

$|=1;
$host=shift||'www.163.com';

$count1=0;
$count2=0;
$count3=0;
$count4=0;
$stime=time;

ti();

while(sleep(1))
{
    $p = Net::Ping->new();
    $p->hires();
if(($ret, $duration, $ip) = $p->ping($host,5.5) and $ret>0)
{
system("cls");
ti();
printf("n   与目标主机:$host [ip:$ip] 保持通讯中 (连接速度: %.2f ms)nn", $duration*1000);

if($duration<1)
{
$count1++;
system("title 网络良好.保持通讯中.目标主机:$host");
}
if($duration<2.5 and $duration>=1)
{
$count2++;
system("title 网络一般.保持通讯中.目标主机:$host");
}
if($duration>=2.5)
{
$count3++;
system("title 网络较差.保持通讯中.目标主机:$host");
}
}
else
{
system("cls");
ti();
print "n   连接到目标主机:$host 失败!n";
system("title 网络异常.连接失败.目标主机:$host");
$count4++;
}
$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("_"x60,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;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N e t w o r k    S p e e d    T e s t
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;By Xtiger www.xtiger.net  Beta Ver0.1
____________________________________________________________
END
}

sub cprin
{
($str,$i)=@_;
cprint("x03" . $i . "$strnx030");
}

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);
}

sub usag
{
ti();
print "使用方法:$0 IPn";
print "例如:$0 66.66.66.66n";
exit;
}


[ 本帖最后由 xti9er 于 2009-2-27 14:05 编辑 ]

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
发表于 2009-02-27 14:05 |显示全部楼层
把 Smilies 禁用了吧

论坛徽章:
0
发表于 2009-02-27 14:06 |显示全部楼层
原帖由 MMMIX 于 2009-2-27 14:05 发表
把 Smilies 禁用了吧





老大,你也太神速了吧

我才发了没半分钟你就跟进了。已改

论坛徽章:
0
发表于 2009-02-27 14:32 |显示全部楼层
另外一种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);
}

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
发表于 2009-02-27 14:35 |显示全部楼层
原帖由 xti9er 于 2009-2-27 14:06 发表





老大,你也太神速了吧

我才发了没半分钟你就跟进了。已改

呵呵,凑巧了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP