user786 has asked for the wisdom of the Perl Monks concerning the following question:
I have written a couple of functions for tailing the logs and grep for a string.
I grep for the string 100.81.2.59:4500', 'dst:172.25.150.190:4500'.
When the function is called the logs are tailed as expected,The above mentioned strings are also present in the logs but the function always returns false.
WARN Not found "string" n ipsecd.log
Function:
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; }
Function call : ,
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'}, @ch +eckStrings); 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] processOutbou +nd: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.5 +9:4500 dst:172.25.150.190:4500 [04-15 21:17:04.821548 (05026) D ipsec_processor.: 496] processOutbou +nd: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.5 +9:4500 dst:172.25.150.190:4500 [04-15 21:17:05.029262 (05026) D ipsec_processor.: 496] processOutbou +nd: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.5 +9:4500 dst:172.25.150.190:4500 [04-15 21:17:05.237628 (05026) D ipsec_processor.: 496] processOutbou +nd: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.5 +9:4500 dst:172.25.150.190:4500 [04-15 21:17:05.444636 (05026) D ipsec_processor.: 496] processOutbou +nd: IPsec EDP-UDP (NAT-T) packet to destination - src addr:100.81.2.5 +9:4500 dst:172.25.150.190:4500
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl: Function to search for a string
by CountZero (Bishop) on Apr 21, 2015 at 16:10 UTC | |
|
Re: Perl: Function to search for a string
by choroba (Cardinal) on Apr 21, 2015 at 15:22 UTC | |
by user786 (Sexton) on Apr 21, 2015 at 15:27 UTC | |
by choroba (Cardinal) on Apr 21, 2015 at 15:30 UTC | |
|
Re: Perl: Function to search for a string
by marinersk (Priest) on Apr 22, 2015 at 03:08 UTC |