|
楼主 |
发表于 2021-2-26 17:07:54
|
显示全部楼层
然后编写脚本以发送统计报告以进行博客访问。
#!/usr/bin/perl
use strict;
use POSIX;
use Email::Send::YYClouds;
use LWP::Simple;
use JSON;
use utf8;
my $date = strftime "%F",localtime;
my $subject = "Daily visit statistics - $date";
my $log = "/var/log/apache2/access.log";
my %hash;
open HD,$log or die $!;
while (<HD>) {
# a regex for matching the correct visit IP
next unless m{^(\d+\.\d+\.\d+\.\d+).*\"GET /.*?/ HTTP/};
my $ip=$1;
$hash{$ip} ++;
}
close HD;
my $body = '<table style="width:100%">' . "\n";
for my $ip (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
my $num = $hash{$ip};
my $loc ="";
if ($num>=2) {
# get IP location via public api
$loc = ip_loc($ip);
}
$body .= <<DATA;
<tr>
<td>$ip</td>
<td>$num</td>
<td>$loc</td>
</tr>
DATA
}
$body .= '</table>';
#
# send email
#
my $msg = Email::Send::YYClouds->new();
$msg->send(recepient => ['wesley@sample.com'],
sender => 'info@myhostnames.com',
smtprelay => 'localhost',
subject => $subject,
body => $body,
);
#
# ip query
#
sub ip_loc {
my $ip = shift;
my $content = get("https://ipapi.co/$ip/json/");
if (defined $content) {
my $json = decode_json($content);
return $json->{city}. ", " . $json->{region}. ", ". $json->{country};
} else {
return "";
}
}
前面都做完了,这个部分需要怎么修改以便执行,谢谢大神
|
|