Search code examples
perlinotify

Monitor a log file for Mac Address using Perl and Linux::Inotify2 Module


I currently have my script here it is, my goal is to be able to monitor a live log file that is updated every second and as soon as my script finds this f8:27:93:88:1c:95 mac address it writes the line to a script.

#!/usr/bin/perl
my $mac = "f8:27:93:88:1c:95";
open (OUT, ">output.txt");
sub Reader (){
    @a1 = `Tail System.log`;
}
sub Parser (){
    if( $_=~ m/f8:27:93:88:1c:95/ ){
        print OUT $_;
    }
}

My goal is to be able to watch this log file, it is being updated every second so tail does not work well.

Here is a snippet from the log file

> [2014-07-18 14:11:22,849] <inform_stat-1> WARN event - [event] User[f8:27:93:0c:da:c5] roams from AP[dc:9f:db:1a:61:bd] to AP[dc:9f:db:1a:61:b9] on "channel 44(na)"


Solution

  • Perhaps use a cpan module like File::Tail

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use autodie;
    
    use File::Tail;
    
    my $infile = 'System.log';
    my $outfile = 'output.txt';
    my $mac = 'f8:27:93:88:1c:95';
    
    open my $outfh, '>', $outfile;
    
    my $tail = File::Tail->new($infile);
    while (defined(my $line = $tail->read)) {
        print $outfh $line if $line =~ m/\Q$mac/;
    }