免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7252 | 回复: 8
打印 上一主题 下一主题

每个1秒ping一次 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-12 11:23 |只看该作者 |倒序浏览
俺想每隔1秒ping一次,每个ping动作的timeout值都是3秒,然后记录下做ping这个动作的时刻与ping值
例如:
2009-09-12  08:00:01  pingtime --> timeout
2009-09-12  08:00:02  pingtime --> timeout
2009-09-12  08:00:03  pingtime --> 3ms
2009-09-12  08:00:05  pingtime --> timeout
2009-09-12  08:00:04  pingtime --> 4ms


以下是俺目前的代码,不能实现这个功能,请各位指点下

use strict;
use warnings;
use Net::Ping;

my $complete_IP = '131.1.1.1';
my $p = Net::Ping->new("icmp");
$p->hires(1);
while (1) {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
    my $cur_time = sprintf "%4d-%02d-%02d %02d:%02d:%02d",
        $year+1900,$mon+1,$mday,$hour,$min,$sec;
    printf "$cur_time  PingTime  -->  ";
    my $pid = fork();
    if ( not defined $pid ) {
        print "cannot fork\n";
    }elsif ( $pid == 0 ) {
        my @resp = $p->ping($complete_IP,3);
        if ( $resp[0] ) {
            my $ms = $resp[1] * 1000;
            my $pingtime =  sprintf "%.2f" , $ms;
            print "$pingtime ms\n";
        }else{
            print "time out\n";
        }
        exit(0);        
    }else{
        waitpid($pid,0);
    }
    sleep 1;        
    
}






[ 本帖最后由 sosogh 于 2009-9-12 11:25 编辑 ]

论坛徽章:
1
狮子座
日期:2013-12-16 16:09:24
2 [报告]
发表于 2009-09-30 13:09 |只看该作者

回复 #1 sosogh 的帖子

貌似这个问题用perl的多线程模式实现比较好
为了使得显示结果效果更好,稍微用了一点点技巧

      1 #!/usr/bin/perl
      2 use strict;
      3 use warnings;
      4 use threads;
      5 use Net::Ping;
      6 my $complete_IP = '10.1.1.255';   #用了一个不存在的ip试验timeout
      7 my $p = Net::Ping->new("icmp");
      8 $p->hires(1);
      9 $|=1;
     10 sub myping() {
     11         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
     12         my $cur_time = sprintf "%4d-%02d-%02d %02d:%02d:%02d",
     13             $year+1900,$mon+1,$mday,$hour,$min,$sec;
     14         print "$cur_time  PingTime  -->  ";
     15         my $pid = fork();
     16         if ( not defined $pid ) {
     17             warn "cannot fork";
     18         }elsif ( $pid == 0 ) {
     19             print "Time out";
     20             my @resp = $p->ping($complete_IP,3);
     21             if ( $resp[0] ) {
     22                 my $ms = $resp[1] * 1000;
     23                 my $pingtime =  sprintf "%.2f" , $ms;
     24                 print "\r$cur_time  PingTime  -->  $pingtime ms     ";
     25             }else{
     26                 #            $result="Time out";
     27
     28             }
     29             exit(0);
     30         }else{
     31             waitpid($pid,0);
     32         }
     33 }
     34 while(1){
     35     print "\n";
     36     threads->create(\&myping);
     37     sleep (1);
     38     my @threads = threads->list();
     39     #warn $#threads;
     40     foreach (@threads){
     41     $_->detach();
     42     }
     43 }
     44

论坛徽章:
1
狮子座
日期:2013-12-16 16:09:24
3 [报告]
发表于 2009-09-30 13:18 |只看该作者

回复 #2 ttcn 的帖子

ping 不存在的地址...
sudo ./myping.pl

2009-09-30 13:10:02  PingTime  -->  Time out
2009-09-30 13:10:03  PingTime  -->  Time out
2009-09-30 13:10:04  PingTime  -->  Time out
2009-09-30 13:10:05  PingTime  -->  Time out
2009-09-30 13:10:06  PingTime  -->  Time out
2009-09-30 13:10:07  PingTime  -->  Time out
2009-09-30 13:10:08  PingTime  -->  Time out

ping 存在的地址
sudo ./myping.pl

2009-09-30 13:13:04  PingTime  -->  0.60 ms
2009-09-30 13:13:05  PingTime  -->  0.59 ms
2009-09-30 13:13:06  PingTime  -->  0.62 ms
2009-09-30 13:13:07  PingTime  -->  0.62 ms
2009-09-30 13:13:08  PingTime  -->  0.64 ms
2009-09-30 13:13:09  PingTime  -->  0.62 ms
2009-09-30 13:13:10  PingTime  -->  0.62 ms
2009-09-30 13:13:11  PingTime  -->  0.62 ms

论坛徽章:
0
4 [报告]
发表于 2009-10-14 14:20 |只看该作者

回复 #2 ttcn 的帖子

学习了,Net:ing模块的调用和线程的应用。

论坛徽章:
0
5 [报告]
发表于 2009-10-14 14:41 |只看该作者

回复 #1 sosogh 的帖子

为什么都要使用fork()生成子进程呢?不懂。放弃使用多线程,精炼程序如下:
============================================================
use strict;
use warnings;
use Net:ing;

my $complete_IP = '131.1.1.1';
my $p = Net:ing->new("icmp";
$p->hires(1);

while (1) {
    my ($sec,$min,$hour,$mday,$mon,$year)=localtime;
    my $cur_time = sprintf "%4d-%02d-%02d %02d:%02d:%02d",
        $year+1900,$mon+1,$mday,$hour,$min,$sec;
    printf "$cur_time PingTime --> Time out";
    my @resp = $p -> ping($complete_IP,3);
    if ($resp[0])
    {
      my $ms = $resp[1] * 1000;
      my $pingtime =  sprintf "%.2f" , $ms;
      print "\r$cur_time PingTime --> $pingtime ms ";
    }
    print "\n";
    sleep 1;         
}
============================================================
程序在本机上测试通过。

论坛徽章:
0
6 [报告]
发表于 2009-10-14 14:51 |只看该作者

回复 #1 sosogh 的帖子

不知道楼主的具体问题呢,我怎么用你的程序跑通了呢?楼主把错误挂出来吧。

论坛徽章:
5
技术图书徽章
日期:2014-02-10 10:55:18技术图书徽章
日期:2014-03-17 16:37:45狮子座
日期:2014-04-25 11:17:42未羊
日期:2014-08-13 11:45:23天蝎座
日期:2015-12-16 10:30:37
7 [报告]
发表于 2009-10-14 15:39 |只看该作者
我是来参观lz头像的

论坛徽章:
5
技术图书徽章
日期:2014-02-10 10:55:18技术图书徽章
日期:2014-03-17 16:37:45狮子座
日期:2014-04-25 11:17:42未羊
日期:2014-08-13 11:45:23天蝎座
日期:2015-12-16 10:30:37
8 [报告]
发表于 2009-10-14 15:43 |只看该作者
lz需要的程序貌似能ping死服务器

论坛徽章:
1
狮子座
日期:2013-12-16 16:09:24
9 [报告]
发表于 2009-10-14 21:23 |只看该作者

回复 #5 HongLian3 的帖子

没有开$|=1显示会有问题

尝试一个不存在的地址:
2009-10-14 21:17:38 PingTime --> Time out
2009-10-14 21:17:42 PingTime --> Time out
2009-10-14 21:17:46 PingTime --> Time out
2009-10-14 21:17:50 PingTime --> Time out
2009-10-14 21:17:54 PingTime --> Time out^C
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP