亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 7286 | 回復(fù): 8
打印 上一主題 下一主題

每個(gè)1秒ping一次 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-09-12 11:23 |只看該作者 |倒序?yàn)g覽
俺想每隔1秒ping一次,每個(gè)ping動(dòng)作的timeout值都是3秒,然后記錄下做ping這個(gè)動(dòng)作的時(shí)刻與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


以下是俺目前的代碼,不能實(shí)現(xiàn)這個(gè)功能,請(qǐng)各位指點(diǎn)下

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 [報(bào)告]
發(fā)表于 2009-09-30 13:09 |只看該作者

回復(fù) #1 sosogh 的帖子

貌似這個(gè)問(wèn)題用perl的多線程模式實(shí)現(xiàn)比較好
為了使得顯示結(jié)果效果更好,稍微用了一點(diǎn)點(diǎn)技巧

      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';   #用了一個(gè)不存在的ip試驗(yàn)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 [報(bào)告]
發(fā)表于 2009-09-30 13:18 |只看該作者

回復(fù) #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 [報(bào)告]
發(fā)表于 2009-10-14 14:20 |只看該作者

回復(fù) #2 ttcn 的帖子

學(xué)習(xí)了,Net:ing模塊的調(diào)用和線程的應(yīng)用。

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2009-10-14 14:41 |只看該作者

回復(fù) #1 sosogh 的帖子

為什么都要使用fork()生成子進(jìn)程呢?不懂。放棄使用多線程,精煉程序如下:
============================================================
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;         
}
============================================================
程序在本機(jī)上測(cè)試通過(guò)。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2009-10-14 14:51 |只看該作者

回復(fù) #1 sosogh 的帖子

不知道樓主的具體問(wèn)題呢,我怎么用你的程序跑通了呢?樓主把錯(cuò)誤掛出來(lái)吧。

論壇徽章:
5
技術(shù)圖書(shū)徽章
日期:2014-02-10 10:55:18技術(shù)圖書(shū)徽章
日期: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 [報(bào)告]
發(fā)表于 2009-10-14 15:39 |只看該作者
我是來(lái)參觀lz頭像的

論壇徽章:
5
技術(shù)圖書(shū)徽章
日期:2014-02-10 10:55:18技術(shù)圖書(shū)徽章
日期: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 [報(bào)告]
發(fā)表于 2009-10-14 15:43 |只看該作者
lz需要的程序貌似能ping死服務(wù)器

論壇徽章:
1
獅子座
日期:2013-12-16 16:09:24
9 [報(bào)告]
發(fā)表于 2009-10-14 21:23 |只看該作者

回復(fù) #5 HongLian3 的帖子

沒(méi)有開(kāi)$|=1顯示會(huì)有問(wèn)題

嘗試一個(gè)不存在的地址:
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
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP