File :: Tail ::选择什么是@pending行为

时间:2011-03-31 05:02:58

标签: perl filehandle

foreach my $subfile(glob "*:*")

    {
            print "opening $subfile\n";
            push(@files,File::Tail->new(name=>"$subfile",debug=>$debug));
    }
    while (1)
    {
            ($nfound,$timeleft,@pending)= File::Tail::select(undef,undef,undef,$timeout,@files);
            unless ($nfound)
            {
               # timeout - do something else here, if you need to
            }
            else
            {
                    foreach (@pending)
                    {
                            print $_->{"input"}." (".localtime(time).") ".$_-> read;
                    }
            }
    }

@pending行为是什么?如果结果显示如下......

TCP:34628-80 (Wed Mar 30 01:49:57 2011) 03/30-01:49:50.607858 119.40.116.196:80 -> 192.168.242.133:34628
TCP:34628-80 (Wed Mar 30 01:49:57 2011) TCP TTL:128 TOS:0x0 ID:34869 IpLen:20 DgmLen:40
TCP:34629-80 (Wed Mar 30 01:49:57 2011) 03/30-01:49:51.309716 119.40.116.196:80 -> 192.168.242.133:34629
UDP:41415-53 (Wed Mar 30 01:49:57 2011) 03/30-01:49:47.220999 192.168.242.2:53 -> 192.168.242.133:41415
UDP:44705-53 (Wed Mar 30 01:49:57 2011) 03/30-01:49:47.427011 192.168.242.2:53 -> 192.168.242.133:44705
UDP:50539-53 (Wed Mar 30 01:49:57 2011) 03/30-01:49:47.213455 192.168.242.2:53 -> 192.168.242.133:50539
TCP:34628-80 (Wed Mar 30 01:49:57 2011) ***AP**F Seq: 0x2F3E700A  Ack: 0x2359814F  Win: 0xFAF0  TcpLen: 20
TCP:34629-80 (Wed Mar 30 01:49:57 2011) TCP TTL:128 TOS:0x0 ID:34871 IpLen:20 DgmLen:40
UDP:41415-53 (Wed Mar 30 01:49:57 2011) UDP TTL:128 TOS:0x0 ID:34859 IpLen:20 DgmLen:65
UDP:44705-53 (Wed Mar 30 01:49:57 2011) UDP TTL:128 TOS:0x0 ID:34861 IpLen:20 DgmLen:153
UDP:50539-53 (Wed Mar 30 01:49:57 2011) UDP TTL:128 TOS:0x0 ID:34857 IpLen:20 DgmLen:179
TCP:34628-80 (Wed Mar 30 01:49:57 2011) =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
TCP:34629-80 (Wed Mar 30 01:49:57 2011) ***AP**F Seq: 0x9D70418  Ack: 0x248089DB  Win: 0xFAF0  TcpLen: 20
UDP:41415-53 (Wed Mar 30 01:49:57 2011) Len: 37
UDP:44705-53 (Wed Mar 30 01:49:57 2011) Len: 125
UDP:50539-53 (Wed Mar 30 01:49:57 2011) Len: 151
TCP:34628-80 (Wed Mar 30 01:49:57 2011) 
TCP:34629-80 (Wed Mar 30 01:49:57 2011) =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

是不是@pending将逐行存储数据?我如何修改代码以仅检索IP地址?在我看来,我认为我可以使用split()来逐行打破这一行。 .so任何人都可以给我一些关于它的例子

2 个答案:

答案 0 :(得分:0)

正则表达式是获取IP地址的最简单方法:

if (/([.\d]+):\d -> ([.\d]+):\d/) {
     # ipaddress 1 will be in $1, and ip address 2 will be in $2
}

答案 1 :(得分:0)

!在/ usr / local / bin中/ perl的

使用File :: Tail;

chdir(“/ var / log / snort”);

预告我的$ fol(glob“。*。*”) {

    print "Opening $fol\n";

    chdir("/var/log/snort/$fol");

    foreach my $subfile(glob "*:*")
    {

            print "opening $subfile\n";

            push(@files,File::Tail->new(name=>"$subfile",debug=>$debug));
    }

    while (1)
    {
            ($nfound,$timeleft,@pending)= File::Tail::select(undef,undef,undef,$timeout,@files);
            unless ($nfound)
            {
                    print "Nothing to print \n";
            }
            else

foreach(@pending)                         {                                if(/([.\d]+):\d - >([。\ d] +):\ d /)                                 {

                                    print $_->{"input"}." (".localtime(time).") ".$2 -> read;
                                                                  }
                    }
            }
    }

} 嘿,我这样做的人......这不正确吗?看起来没什么好打印出来的