I
I
IndiNoob2020-05-25 20:12:38
Perl
IndiNoob, 2020-05-25 20:12:38

How to get IP from logs in PERL 6 (Raku)?

How to extract a list of IPs from the log and group by number?
log example:

185.49.11.195 - - [21/Sep/2015:08:57:09 +0200] "GET http://testp1.moelno.lubin.pl/testproxy.php HTTP/1.1
499 0 "-" "Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/31.0"
54.224.44.192 - - [21/Sep/2015:11:01:51 +0200] "GET / HTTP/1.1" 499 0 "-" "Cloud mapping experiment searching for shoutcast servers. Contact [email protected]"
54.214.23.199 - - [21/Sep/2015:11:01:51 +0200] "HEAD / HTTP/1.1" 499 0 "-" "Cloud mapping experiment. Contact [email protected]"
143.56.71.195 - - [21/Sep/2015:13:07:45 +0200] "HEAD /robots.txt HTTP/1.0" 499 0 "-" "-"
143.56.71.195 - - [21/Sep/2015:13:55:35 +0200] "GET / HTTP/1.1" 499 0 "-" "Python-urllib/2.7"
143.56.71.195 - - [21/Sep/2015:13:55:46 +0200] "GET /script HTTP/1.1" 499 0 "-" "Python-urllib/2.7"
143.56.71.195 - - [21/Sep/2015:13:55:56 +0200] "GET /jmx-console HTTP/1.1" 499 0 "-" "Python-urllib/2.7"
143.56.71.195 - - [21/Sep/2015:13:56:06 +0200] "GET /msd HTTP/1.1" 499 0 "-" "Python-urllib/2.7"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
humanitatium, 2020-06-23
@humanitatium

Of course, I understand that this is a thick trolling, which I accidentally stumbled upon a month later, but it is useless to have a question without an answer. Yes, there are people who are learning perl6. Yes, it's still super-easy to do such things in perl6. You can even parse this log in one pass with a beautiful grammar, but this is an overkill solution, because you only need IP.

#!/usr/bin/env perl6
#определим октет как \d{1,3}
my $o = /<digit> ** 1..3/; 
#определим IP как 4 октета, разделенных точкой
my $ipdef = /$o '.' $o '.' $o '.' $o/;

#объявим хэщ
my %ip_list;
#заполним его IP-шниками
%ip_list{$_ ~~ $ipdef}++ if /$ipdef/ for 'log.txt'.IO.lines;
#выведем на экран, сортировав по убыванию частоты айпи в логе, флудеры сверху
.say for %ip_list.sort: { $^b.value cmp $^a.value };

#а вот так по возрастанию
#  .say for %ip_list.sort: *.value;

Without explanation, it's 5 lines.
Result:
143.56.71.195 => 5
54.214.23.199 => 1
54.224.44.192 => 1
185.49.11.195 => 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question