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
    Are you sure your grep subroutine gets the correct data? Perhaps you can print the data that was passed to the subroutine to make sure.

    Please, note that the '.' (dot) that is part of the strings to check for will be considered a regex-wildcard character. To avoid this you can quote-meta this string in the regex: $logLines =~ /\Q$string\E/.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Perl: Function to search for a string
by choroba (Cardinal) on Apr 21, 2015 at 15:22 UTC
    Crossposted at StackOverflow. It's considered polite to inform about crossposting so people not attending both sites don't waste their efforts hacking a solution to a problem already solved at the other end of the Internet.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Answer provided in stack over flow didn't work .Infact no answer was provided. My code was modified a bit and reposted again. which didn't help
        Help us to help you. Posting code that we can't run, or code that we run after some changes without the symptoms you describe, doesn't do it. Short, Self Contained, Correct Example explains the details.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl: Function to search for a string
by marinersk (Priest) on Apr 22, 2015 at 03:08 UTC

    Please pay attention to all the advice given to you thus far -- it will help you in future attempts to seek help here.

    In the meantime, I suspect this is your problem:

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

    If my gut feel be aright, the problem here is that you are feeding it a string where a regular expression is expected.

    Consider using quotemetato convert:

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

    But this is just a guess -- all the other advice on this thread should be followed if this does not solve your problem. As a rule, we can't fix what we can't run.