simple script for monitoring a mailserver
You can use nc
to test a SMTP mail server like so:
$ nc -w 5 mail.mydom.com 25 << EOF
HELO mail.mydom.com
QUIT
EOF
NOTE: The options -w 5
tell nc
to wait at most 5 seconds. The server to monitor is mail.mydom.com
and 25
is the port we're connecting to.
You can also use this form of the above if you find your server is having issues with the HELO
:
$ echo "QUIT" | nc -w 5 mail.mydom.com 25
NOTE: This form works well with both Postfix and Sendmail!
Example
Here I'm connecting to my mail server.
$ echo "QUIT" | nc -w 5 mail.bubba.net 25
220 bubba.net ESMTP Sendmail 8.14.3/8.14.3; Sat, 19 Apr 2014 16:31:44 -0400
221 2.0.0 bubba.net closing connection
$
If you check the status returned by this operation:
$ echo $?
0
However if nothing at the other ends accepts our connection:
$ echo QUIT | nc -w5 localhost 25
Ncat: Connection refused.
$
Checking the status returned from this:
$ echo $?
1
Putting it together
Here's my version of a script called mail_chkr.bash
.
#!/bin/bash
echo "Checking Mail Server #1"
echo "QUIT" | nc -w 5 mail.bubba.net 25 > /dev/null 2>&1
if [ $? == 0 ]; then
echo "mail server #1 is UP"
else
echo "mail server #1 is DOWN"
fi
echo "Checking Mail Server #2"
echo "QUIT" | nc -w 5 localhost 25 > /dev/null 2>&1
if [ $? == 0 ]; then
echo "mail server #2 is UP"
else
echo "mail server #2 is DOWN"
fi
Running it:
$ ./mail_chkr.bash
Checking Mail Server #1
mail server #1 is UP
Checking Mail Server #2
Ncat: Connection refused.
mail server #2 is DOWN
Related videos on Youtube
user1968963
Updated on September 18, 2022Comments
-
user1968963 almost 2 years
I would like to use a bash script (python would be second best) to monitor regularly (hourly) if my mailserver is online and operating.
I know that there are dedicated solutions for this task (Nagios, ...) but I really need something simple that I can use as a cronjob. Only to see the mailserver is alive.
I know how to talk with a mailserver with telnet, ie:
telnet mail.foo.org 25 EHLO example.com mail from: rcpt to: ...
but this is interactive. Is it possible to check with a script that the mailserver is communicating? Obviously, I don't want to go the whole way and actually send an email. I just want to test that the mailserver is responding.
-
Admin about 10 yearsIs checking if the server is enough? Does the mailserver can crash alone?
-
Admin about 10 yearsCheck out the code snippet in my answer here
-
Admin about 10 yearsA system management tool like chef or puppet would be a good tool to make sure it stays running.
-
-
Bananguin about 10 yearsI think it's cleaner to send a single
.
to terminate the SMTP session, rather thanEOF
after theHELO
. -
slm about 10 years@Bananguin - actually when I tried that it gave me this error:
500 5.5.1 Command unrecognized: "."
. Adding a QUIT however worked, so I'll add that. -
user1968963 about 10 yearsWorks great. I have added
-w 5
fornc
as a timeout of 5 sec. Otherwise it seems to hang indefinitely when it cannot reach the server. -
slm about 10 years@user1968963 - Yes I meant to mention that. I'll add it into the answer.
-
user1968963 about 10 years@slm - on the mailserver, I see these messages in the log:
postfix/smtpd improper command pipelining after HELO from unknown: QUIT\n
. Can we get rid of them, or is postfix just complaining that I hang up prematurely? -
user1968963 about 10 years@slm - a dot
.
does not work even in telnet. And without theQUIT
the connection hangs until the specified 5 seconds elapse. I think, postfix does not like that theQUIT
arrives together with theHELO
. -
slm about 10 years@user1968963 - I use sendmail. Have never used postfix. The dot was suggested previously so I thought perhaps that would work here. I believe the standard RFC for SMTP says to use
QUIT
. -
user1968963 about 10 years@slm - is there any chance of getting rid of the error message?
-
slm about 10 years@user1968963 - in searching for a solution I'm coming up empty. This seems to be a bit of an issue with Postfix, best I can tell.
-
user1968963 about 10 years@slm - when I get rid of the
HELO
part and only leaveQUIT
I get no error and it seems to work fine. Thus, my minimalistic solution:echo QUIT | nc -w5 mail.bubba.net 25 ; echo $?
-
slm about 10 years@user1968963 - that works for me too. I'll incorporate that into the overall A. Thanks!
-
slm about 10 years@user1968963 - updated!