#!/usr/bin/perl
use strict;
use warnings;
use Net::Subnet;
my $filename = 'mylogFile.log';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
my @cidr_list = <$fh>;
my $matcher = subnet_matcher qw(192.168.1.0/22);
my @grepIPs = grep(/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\./,@cidr_list);
for (my $i=0; $i < scalar @grepIPs; $i++){
my $boolean = $matcher->($grepIPs[$i]);
if ($boolean == 1){
print $grepIPs[$i];
}
else{
print "Nothing\n";
}
}
####
192.168.1.2
192.168.1.3
192.168.1.4
####
2017-12-08 07:01:39 GET /course-detail.aspx id=66&catColor=0 443 - 192.168.1.0 (e.g., IP) 200 0 0 530
####
Sample output that I want to achieve:
2017-12-08 07:01:39 GET /course-detail.aspx id=66&catColor=0 443 - 192.168.1.1 (e.g., IP) 200 0 0 530
2017-12-08 07:01:39 GET /course-detail.aspx id=66&catColor=0 443 - 192.168.1.2 (e.g., IP) 200 0 0 530
2017-12-08 07:01:39 GET /course-detail.aspx id=66&catColor=0 443 - 192.168.1.3 (e.g., IP) 200 0 0 530
#Note that I only want to grep IP addresses (192.168.1.1-3) that are in the same netmask specified in $matcher