dotowwxo has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, currently i have this script that looks for IP address that is in the netmask specified in $matcher and print that line from mylogFile.log
#!/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"; } }
However, $grepIPS only matches lines that only have IP address specified without any other strings in that particular line.
For e.g., my script only matches lines that only has IP address:
192.168.1.2 192.168.1.3 192.168.1.4
it does not matches lines that has other strings involved as shown below:
2017-12-08 07:01:39 <my source IP> GET /course-detail.aspx id=66&catCo +lor=0 443 - 192.168.1.0 (e.g., IP) <server version etc. strings> 200 +0 0 530
What I want as my output: I want to grep only the IP addresses (not the whole line which I can't feed into my for loop to check) from the line so that I can feed it into my for loop to see if the IP is in the netmask, and if yes, I would like to print the whole line. Can anyone guide me on how I can achieve this? any response is kindly appreciated, thank you.
Sample output that I want to achieve: 2017-12-08 07:01:39 <my source IP> GET /course-detail.aspx id=66&catCo +lor=0 443 - 192.168.1.1 (e.g., IP) <server version etc. strings> 200 +0 0 530 2017-12-08 07:01:39 <my source IP> GET /course-detail.aspx id=66&catCo +lor=0 443 - 192.168.1.2 (e.g., IP) <server version etc. strings> 200 +0 0 530 2017-12-08 07:01:39 <my source IP> GET /course-detail.aspx id=66&catCo +lor=0 443 - 192.168.1.3 (e.g., IP) <server version etc. strings> 200 +0 0 530 #Note that I only want to grep IP addresses (192.168.1.1-3) that are i +n the same netmask specified in $matcher
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to grep matching IP address from a log file?
by siberia-man (Friar) on Dec 19, 2017 at 09:21 UTC | |
by dotowwxo (Acolyte) on Dec 19, 2017 at 09:42 UTC | |
|
Re: How to grep matching IP address from a log file?
by haukex (Archbishop) on Dec 19, 2017 at 07:46 UTC | |
by dotowwxo (Acolyte) on Dec 19, 2017 at 08:19 UTC | |
by haukex (Archbishop) on Dec 19, 2017 at 13:18 UTC | |
by hippo (Archbishop) on Dec 19, 2017 at 09:02 UTC | |
|
Re: How to grep matching IP address from a log file?
by LanX (Saint) on Dec 19, 2017 at 07:42 UTC | |
|
Re: How to grep matching IP address from a log file?
by Anonymous Monk on Dec 19, 2017 at 21:41 UTC |