Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to grep matching IP address from a log file?

by siberia-man (Friar)
on Dec 19, 2017 at 09:21 UTC ( [id://1205846]=note: print w/replies, xml ) Need Help??


in reply to How to grep matching IP address from a log file?

Hi,

I understand that gurus have responded you. Nevertheless, I'd like to add my 5 cents. What I've seen as a solution after I completed to read the thread.

1. grep input for any lines matching IP pattern
2. IPs after (1) should be tested for matching the IP list from the specified subnet

Before posting my answer I created tiny toy proving correctness of my approach:
#!/usr/bin/perl use strict; use warnings; use Net::Subnet; my $matcher = subnet_matcher qw(192.168.1.0/22); while ( <DATA> ) { my @ips = m/(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25 +[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/g; next unless @ips; next unless grep { $matcher->($_) } @ips; print; } __DATA__ Sample output that I want to achieve: 2017-12-08 07:01:39 127.0.0.1 GET /course-detail.aspx id=66&catColor=0 + 443 - 127.0.0.1 (e.g., IP) <server version etc. strings> 200 0 0 530 2017-12-08 07:01:39 127.0.0.1 GET /course-detail.aspx id=66&catColor=0 + 443 - 192.168.1.2 (e.g., IP) <server version etc. strings> 200 0 0 5 +30 2017-12-08 07:01:39 127.0.0.1 GET /course-detail.aspx id=66&catColor=0 + 443 - 192.168.2.1 (e.g., IP) <server version etc. strings> 200 0 0 5 +30 2017-12-08 07:01:39 127.0.0.1 GET /course-detail.aspx id=66&catColor=0 + 443 - 192.168.1.3 (e.g., IP) <server version etc. strings> 200 0 0 5 +30 2017-12-08 07:01:39 127.0.0.1 GET /course-detail.aspx id=66&catColor=0 + 443 - 192.168.4.3 (e.g., IP) <server version etc. strings> 200 0 0 5 +30 #Note that I only want to grep IP addresses (192.168.1.1-3) that are i +n the same netmask specified in $matcher
Please pay attention that netmask /22 corresponds to wide range of IPs 192.168.1.0 - 192.168.3.255.

Replies are listed 'Best First'.
Re^2: How to grep matching IP address from a log file?
by dotowwxo (Acolyte) on Dec 19, 2017 at 09:42 UTC
    Hey! That worked perfectly for me. Thank you so much! Really helped me a lot cause I'm a total newbie :')

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1205846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found