好吧,如果你打算使用perl,不妨一路走下去.
以下是计算每个域的消息数量的一种相当不完美的方法:
#!/usr/bin/perl
use strict;
my @mailq = `cat /home/users/rilindo/mailq`; #Had to simulate the output of the mail command here. Change this to the actual mailq path,e.g. /usr/bin/mailq
my %domains = ();
foreach my $m (@mailq) {
$m =~ s/^\s+//;
$m =~ s/\s+$//;
$m =~ s/>//;
next if $m =~ /Queue/;
if ($m =~ /\d\d:\d\d:\d\d/) {
$domains{(split(/@/,$m))[1]}++;
}
else {
$domains{(split(/@/,$m))[1]}++;
}
}
foreach my $d (keys %domains) {
print $d . " has $domains{$d} message(s)" . "\n";
}
本质上,我们将mailq命令的输出发送到数组并进行迭代.对于数组中的每个记录,我们剥离前导和尾随空格/换行符,然后在“@”符号处拆分.然后我们插入域作为键(如果它不存在),然后在哈希中递增它.在下一次尝试时,如果找到相同的域,它将简单地增加值.从那里,我们循环遍历哈希,然后打印出具有匹配总数的域.
结果:
[rilindo@localhost ~]$./parsemail.pl
domain.com has 6 message(s)
domain3.com has 2 message(s)
domain1.com has 2 message(s)
就像我说的那样,它并不完美,但它确实起到了作用.如果没有别的,它会让你知道从这里去哪里.
顺便说一下,既然看起来你知道perl,那么对Perl的哈希数据结构的回顾将证明是非常有用的:
要么:
本文介绍了一种使用Perl脚本来解析邮件队列的方法。通过读取邮件队列输出,脚本能够统计每个域名下的邮件数量,并输出统计结果。
288

被折叠的 条评论
为什么被折叠?



