Jump to content

Regex to match log entry


jmurch

Recommended Posts

 

I am trying to come up with a regex that will match this log entry:

 

[Aug 04 14:22:02]      err: bad or missing CDL header. refused connection from 98.165.54.217:54335 [l:980]

 

Where timestamp and ip address will obviousley be different each time.  I dont care about anything after the colon in the ip address.

 

TIA, Jeff

 

Link to comment
Share on other sites

More than one line would be helpful, but

 

$expression = '/\[([^]]+)\] +([^\r\n]+)/';

 


\[([^]]+)] +([^\r\n]+)

Match the character “[” literally «\[»
Match the regular expression below and capture its match into backreference number 1 «([^]]+)»
   Match any character that is NOT a “]” «[^]]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “]” literally «]»
Match the character “ ” literally « +»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 2 «([^\r\n]+)»
   Match a single character NOT present in the list below «[^\r\n]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      A carriage return character «\r»
      A line feed character «\n»

Link to comment
Share on other sites

/bad or missing CDL header\. refused connection from ([0-9.:]+)/

 

I took out err: because it's erroneous, and I'm sure there are other instances of err: that won't match. That'll cause backtracking and slow down your expression.

Link to comment
Share on other sites

 

More missing details:

 

 

This rule matches this ssh log entry:

Log entry: Aug  4 20:59:16 socketserver sshd[21442]: Failed password for root from 98.165.54.217 port 56595 ssh2

Rule: ^%(__prefix_line)sFailed (?:password|publickey) for .* from <HOST>(?: port \d*)?(?: ssh\d*)?$

(Note that this is for use in the iptables module fail2ban. The entry __prefix_line comes from another config file but I figured that leaving it out would be more confusing)

 

This rule does not match this log entry:

Log entry: [Aug 04 14:22:02]      err: bad or missing CDL header. refused connection from 98.165.54.217:54335 [l:980]

Rule: /bad or missing CDL header\. refused connection from ([0-9.:]+)/

 

Thanks!

Link to comment
Share on other sites

I'm not familiar with fail2ban, but I know my RegEx works.

 

<?php


$sb = '[Aug 04 14:22:02]      err: bad or missing CDL header. refused connection from 98.165.54.217:54335 [l:980]';
$ex = '/bad or missing CDL header\. refused connection from ([0-9.:]+)/';
preg_match_all( $ex, $sb, $match );

print_r( $match );

?>

 

Output

 

Array
(
    [0] => Array
        (
            [0] => bad or missing CDL header. refused connection from 98.165.54.217:54335
        )

    [1] => Array
        (
            [0] => 98.165.54.217:54335
        )

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.