分类: LINUX
2010-04-22 12:03:22
postfix:删除队列中 from/to 某个地址的邮件(perl)
最简单的就是 : postsuper -d ALL
作为Postfix MTA的管理员,维护队列是家常便饭
,但如何能够方便的按自己的意愿控制对列呢?这需要一点perl和regexp的知识。
以下提供一个perl的小程序,由一个国外朋友写的,通过命令行传递正则表达式
,匹配的邮件将被删除。
queue_mgr.pl
#!/usr/bin/perl$REGEXP = shift || die "no email-adress given (regexp-style, e.g. bl.*\@yahoo.com)!";@data = qx-p; for (@data) { if (/^(\w+)\*?\s/) { $queue_id = $1; } if($queue_id) { if (/$REGEXP/i) { $Q{$queue_id} = 1; $queue_id = ""; } } } open(POSTSUPER,"| postsuper -d -") || die "couldn't open postsuper" ; foreach (keys %Q) { print POSTSUPER "$_\n"; }; close(POSTSUPER);
还有一种方法:
mailq | awk {print $1}' | tr -d '*' | xargs -n 1 postsuper -d
换成你要删除的email address.
以下是另外一个 脚本:
#!/usr/bin/perl -w # # pfdel - deletes message containing specified address from # Postfix queue. Matches either sender or recipient address. # # Usage: pfdel # use strict; # Change these paths if necessary. my $LISTQ = "/usr/sbin/postqueue -p"; my $POSTSUPER = "/usr/sbin/postsuper"; my $email_addr = ""; my $qid = ""; my $euid = $>; if ( @ARGV != 1 ) { die "Usage: pfdel \n"; } else { $email_addr = $ARGV[0]; } if ( $euid != 0 ) { die "You must be root to delete queue files.\n"; } open(QUEUE, "$LISTQ |") || die "Can't get pipe to $LISTQ: $!\n"; my $entry = ; # skip single header line $/ = ""; # Rest of queue entries print on # multiple lines. while ( $entry = ) { if ( $entry =~ / $email_addr$/m ) { ($qid) = split(/\s+/, $entry, 2); $qid =~ s/[\*\!]//; next unless ($qid); # # Execute postsuper -d with the queue id. # postsuper provides feedback when it deletes # messages. Let its output go through. # if ( system($POSTSUPER, "-d", $qid) != 0 ) { # If postsuper has a problem, bail. die "Error executing $POSTSUPER: error " . "code " . ($?/256) . "\n"; } } } close(QUEUE); if (! $qid ) { die "No messages with the address <$email_addr> " . "found in queue.\n"; } exit 0;