有一个简单的命令来找出在linux发送邮件队列中的每个域的消息的当前数量? mailq转储出一个详细的列表,但它不方便快速浏览。
我正在使用Centos和sendmail。
mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c But the above command output is as following: 1 domain.com>
以上指挥不符合我的要求,请在这方面提供任何帮助。
这里是更新的输出:
domain.com> has 5 message(s) domain.com.pk> has 1 message(s) abc.com.pk> has 2 message(s) xyz.coinfo.net.cn> has 1 message(s) mmm.com> has 1 message(s)
那么,如果你打算使用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, eg /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命令的输出发送到一个数组并进行迭代。 对于数组中的每个logging,我们将前导和尾随空格/换行符分开,然后在“@”符号处进行拆分。 然后我们插入域作为一个关键(如果它不存在),然后在散列中增加它。 在下一次尝试中,如果find相同的域,它将简单地递增该值。 从那里,我们通过哈希循环,然后用匹配的总数打印出域。
结果:
[rilindo@localhost ~]$ ./parsemail.pl domain.com has 6 message(s) domain3.com has 2 message(s) domain1.com has 2 message(s)
就像我说的那样,它不是完美的,但它做的工作。 如果没有别的,它会给你一个从这里走的想法。
顺便说一句,既然看起来你知道perl,那么查看Perl的哈希数据结构将被certificate是非常有用的:
http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
要么:
尝试这个:
# mailq -v | awk 'BEGIN { FS = "@" } \ !/^[a-zA-Z0-9-]|^[ \t]+(\(|\/|Total requests:)/ { print $2 }' | sort | uniq -c
“简单”是相对的。 mailq的输出是一个皇家的痛苦parsing,但它可以做到。 典型的mailq详细输出是这样的:
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient------- 637F5CFF9C* 1497 Sat Dec 18 21:40:34 [email protected] [email protected] 637F5CFF9d* 1497 Sat Dec 18 21:40:35 [email protected] [email protected]
一些创造性的hackery可以让你得到你所需要的:
首先,您要删除最上面的一行 – 这对于人来说是有用的,但与parsing目标无关。
最简单的方法是: mailq -v | egrep -v '^-' mailq -v | egrep -v '^-'
现在您要抓取收件人信息并提取域名。 Perl是你的朋友 – 通过这个方便的脚本pipe理输出(我们称之为get_domains.pl ):
#!/usr/bin/perl $/ = "\n\n"; # Use a blank line as the separator. while (<>) { ($x,$recip) = split(/\n/, $_); # Extract the recipient line ($user,$domain) = split(/@/, $recip); # Get the domain. print "$domain\n"; # Print Recipient Domain }
这只是简单的部分 – 统计域(通过sort | uniq -cpipe道)。
所以mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c会给你输出如下所示:
1 domain.com 1 domain2.com