2015-04-16 50 views
0

我已经写了几个函数来为一个字符串拖尾日志和grep。 我用grep的字符串100.81.2.59:4500', 'dst:172.25.150.190:4500'。当函数被调用日志是否如预期般尾,上面提到的字符串也出现在日志中,但该函数总是返回Perl函数来grep一个字符串

WARN Not found "string" n ipsecd.log 

功能:

sub tail_logs{ 

    my ($self) = @_; 
    my $cmd = 'tail -n 500 /a/logs/ipsecd.log | grep NAT'; 
    $self->execute('$cmd'); 

    if ($self->execute($cmd)) { 
     return $self->get_stdout(); 
     } 
    else { 
      die " Failed to execute $cmd"; 
     } 
} 


sub grep { 
    my ($self,$logLines, @strings) = @_; 

    for my $string (@strings) { 
     if ($logLines =~ /$string/) { 
      INFO("Found $string in ipsecd.log"); 
     } 
     else { 
      #return false, we cound find $string 
      WARN("Not Found $string in ipsecd.log"); 
      return 0; 
     } 
    } 
    # found all strings in the $loglines, return true. 
    return 1; 

} 

函数调用:

my @checkStrings = ('100.81.2.59:4500', 'dst:172.25.150.190:4500'); 

    $self->{'log'} = $self->{'log_obj'}->tail_logs(); 

    $self->{'log_verify'}= $self->{'log_obj'}->grep($self->{'log'}, @checkStrings); 

    if ($self->{'log_verify'}) { 
     $self->assert(
      $self->{'log_verify'}, 
      'Found info in ipsecd.logs' 
     ); 

Loglines

[04-15 21:17:04.614251 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:04.821548 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:05.029262 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:05.237628 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:05.444636 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
+0

请提供一个[简短,独立,正确的例子](http://sscce.org)。你使用什么模块? – choroba

+0

@choroba我没有使用perl cpan模块。这两个函数是我创建的库的一部分,我从我的测试脚本中调用了这两个函数。 – user3587025

+0

http://www.perlmonks.org/?node_id=1124151 – choroba

回答

0

我不知道你使用了什么库,但是在简化你的代码后,我无法复制你描述的行为。

#!/usr/bin/perl 
use warnings; 
use strict; 

{ package My::L; 

    sub new { bless {}, shift } 

    sub tail_logs{ 
     my $cmd = "tail -n 5 $0 | grep NAT"; 
     my $o = `$cmd`; 
     return $o 
    } 


    sub Grep { 
     my ($self, $logLines, @strings) = @_; 

     for my $string (@strings) { 
      if ($logLines =~ /$string/) { 
       warn "INFO: Found $string in ipsecd.log"; 
      } else { 
       warn "WARN: Not Found $string in ipsecd.log"; 
       return 0 
      } 
     } 
     return 1 
    } 
} 

my $self; 
$self->{log_obj} = 'My::L'->new; 

my @checkStrings = ('100.81.2.59:4500', 'dst:172.25.150.190:4500'); 

$self->{log} = $self->{log_obj}->tail_logs(); 
$self->{log_verify} = $self->{log_obj}->Grep($self->{log}, @checkStrings); 
warn 'Found info in ipsecd.logs' if $self->{log_verify}; 

__DATA__ 
[04-15 21:17:04.614251 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:04.821548 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:05.029262 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:05.237628 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 
[04-15 21:17:05.444636 (05026) D ipsec_processor.: 496] processOutbound: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.59:4500 dst:172.25.150.190:4500 

注意'$cmd'不进行内插变量,使用

$self->execute($cmd); 

没有单引号。

另请注意grep是一个Perl函数。你真的想为你的方法命名吗?

0

由于对@choroba引用的PerlMonks线程指出,我怀疑这可能是你的问题:

for my $string (@strings) { 
    if ($logLines =~ /$string/) { 

如果我的直觉是正确地,这里的问题是,你喂养它一个字符串,其中一个正则表达式是预期的。

考虑使用quotemeta转换:

for my $string (@strings) { 
    my $regex = quotemeta $string; 
    if ($logLines =~ /$regex/) { 

但是,这只是一种猜测,因为你似乎没有再被任何人回答的问题。