Saved has asked for the wisdom of the Perl Monks concerning the following question:
We have an Application called 'Tripwire' to monitor Security Compliance & Change. It was set up by another Deptmt. It is based on RegEx's, and my job is to ensure the RegEx's that are in it are working correctly. I've made progress, but have run into an issue I could use help with.
I have a script feed a RegEx from the command line which works.
My test passwd file (To test only root has UID=0)
$ cat passwd root:x:0:0:ifeuu1 root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin noncom:x:0:0:cause RegEx Fail:/noncom:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
The Script
#!/usr/bin/perl # perl-grep4.pl my $pattern = shift @ARGV; my $regex = eval { qr/$pattern/ }; die "Check your pattern! $@" if $@; while( <> ) { if( m/$regex/ ) { print "$_"; print "\t\t\$&: ", substr( $_, $-[0], $+[0] - $-[0] ), "\n"; foreach my $i ( 1 .. $#- ) { print "\t\t\$$i: ", substr( $_, $-[$i], $+[$i] - $-[$i] ), "\n"; } } }
Usage & Output
$ Test5.pl '(?<!\broot:)x:0:' passwd noncom:x:0:0:cause RegEx Fail:/noncom:/bin/bash $&: x:0:
However, trying to get a script which is feed the RegEx from a file is failing
My RegEx-File
PW:root:passwd:(?<!\broot:)x:0:
The script
#!/usr/bin/perl #use strict; #use warnings; use Text::ParseWords; my ($RECORD, @FIELDS, $RE); my $DFile="/home/infosec/data/RegEx"; open (DATA, "<$DFile") || die ("Cannot open DATA file \n"); my @LINES=<DATA>; my $tLINES=@LINES; foreach $RECORD (@LINES[0..$tLINES-1]) { @FIELDS=split(/:/, "$RECORD"); my $TNAME=$FIELDS[0]; my $TVALU=$FIELDS[1]; my $TFILE=$FIELDS[2]; my $REGEX=$FIELDS[3]; chomp($REGEX); my $regex = eval { qr/$REGEX/ }; print "$regex \n"; open (TFILE, "<$TFILE") || die ("Cannot open Test file \n"); while (<TFILE>) { if( m/$regex/ ) { print "$_"; print "\t\t\$&: ", substr( $_, $-[0], $+[0] - $-[0] ), "\n"; foreach my $i ( 1 .. $#- ) { print "\t\t\$$i: ", substr( $_, $-[$i], $+[$i] - $-[$i] ), "\n"; } } } print "\n"; } print "\n";
The output
$ Test6.pl root:x:0:0:ifeuu1 root:/root:/bin/bash $&: bin:x:1:1:bin:/bin:/sbin/nologin $&: noncom:x:0:0:cause RegEx Fail:/noncom:/bin/bash $&: daemon:x:2:2:daemon:/sbin:/sbin/nologin $&: adm:x:3:4:adm:/var/adm:/sbin/nologin $&: lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin $&: sync:x:5:0:sync:/sbin:/bin/sync $&: shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown $&:
This matches all lines instead of only the third, and I had trouble getting strict & warning to run clean. Could anyone help?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Test RegEx
by JavaFan (Canon) on Apr 22, 2010 at 14:47 UTC | |
|
Re: Test RegEx
by MidLifeXis (Monsignor) on Apr 22, 2010 at 15:01 UTC | |
|
Re: Test RegEx
by Marshall (Canon) on Apr 22, 2010 at 22:55 UTC | |
by AnomalousMonk (Archbishop) on Apr 22, 2010 at 23:11 UTC |