Script Perl untuk menghapus queue di SMTP Server


Ada kalanya queue di smtp server penuh sehingga membuat trafic di smtp menjadi besar. tetapi terkadang queue yang ada di smtp tersebut adalah email-email yang tidak diperlukan / spam. berikut adalah perintah cli untuk mengetahui jumlah queue yang ada di smtp server :

postqueue -c /etc/postfix2/ -p | awk ‘/^[0-9,A-F]/ {print $7}’ | sort | uniq -c | sort -n

perintah di atas akan menghasilkan list dari jumlah queue dengan format : [TotalQueue] [Sender]
dan berikut adalah script perl untuk menghapus queue tersebut :

#!/usr/bin/perl -w
#
# pfdel - deletes message containing specified address from
# Postfix queue. Matches either sender or recipient address.
#
# Usage: pfdel <email_address>
#

use strict;

# Change these paths if necessary.
my $LISTQ = “/usr/sbin/postqueue -p -c /etc/postfix2/”;
my $POSTSUPER = “/usr/sbin/postsuper -c /etc/postfix2 -d”;

my $email_addr = “”;
my $qid = “”;
my $euid = $>;

if ( @ARGV != 1 ) {
die “Usage: pfdel <email_address>\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);
`echo ‘$qid $entry’ >> /home/adithama/logdel.pl`;
print $qid.”\n”;
#
# Execute postsuper -d with the queue id.
# postsuper provides feedback when it deletes
#
if ( `$POSTSUPER $qid` ) {
# 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;

 

simpan dengan ext .pl (misal : deletequeu.pl) kemudian jalankan script tersebut dengan perintah #perl deletequeue.pl sender (ganti sender dengan address yang akan dihapus.

 

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout /  Ubah )

Foto Google

You are commenting using your Google account. Logout /  Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout /  Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout /  Ubah )

Connecting to %s