是否有一个简单的命令来查找
linux sendmail队列中每个域的当前消息数? 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,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的哈希数据结构的回顾将证明是非常有用的:
http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
要么: