我已经写了几个函数来为一个字符串拖尾日志和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
请提供一个[简短,独立,正确的例子](http://sscce.org)。你使用什么模块? – choroba
@choroba我没有使用perl cpan模块。这两个函数是我创建的库的一部分,我从我的测试脚本中调用了这两个函数。 – user3587025
http://www.perlmonks.org/?node_id=1124151 – choroba