restart_mail_services.pl

Code Index:



NAME

restart_mail_services.pl


DESCRIPTION

Restart mail services, currently Debian Exim4 + SpamAssassin + Clamav.

Make a telnet test to hostname (public IP) and local interface (127.0.0.1) on port 25.


COPYRIGHT

Copyright 2010 Etienne LEMEE <coding AT etilem DOT net>

This piece of code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


#!/usr/bin/perl -w

use strict;
use Sys::Hostname;
use Net::Telnet;

sub message {
    printf "\n=== %s ===\n", uc shift;
}

sub service {
    my ($s, $a) = @_;
    -x $s and system("$s $a");
}

sub display {
    my ($f, $fh) = shift;
    my $m = "content of $f";
    my $h = sprintf "### %s ###\n", '-' x length $m;
    -s $f or return;
    message $m;
    print $h;
    open $fh, '<', $f or die $!;
    print while (<$fh>);
    print $h;
    printf "May I purge %s (size: %d) ? [y/N] : ", $f, -s $f;
    chomp(my $a = <STDIN>);
    if ($a =~ /y/i) { open $fh, '>', $f or die $!; }
    close $fh;
}

sub telport {
    my ($t, $h, $p, $l) = (new Net::Telnet (Telnetmode => 0), @_);
    $t->open(Host => $h, Port => $p);
    printf "%*s => %s", $l, $h, $t->getline;
}

my $log = "/var/log/exim4/paniclog";

my $host = hostname;

my $max = length $host; $max = $max > 9 ? $max + 1 : 10;

message "stopping mail service";

service qw(/etc/init.d/exim4 stop);

message "restarting helpers";

service qw(/etc/init.d/spamassassin restart);

service qw(/etc/init.d/clamav-daemon restart);

message "starting mail service";

service qw(/etc/init.d/exim4 start);

message "testing mail ports";

telport hostname, 25, $max;

telport "127.0.0.1", 25, $max;

display $log;