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

In reply to Perl: Function to search for a string by user786

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.