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.
Best Answer
You can use
nc
to test a SMTP mail server like so:NOTE: The options
-w 5
tellnc
to wait at most 5 seconds. The server to monitor ismail.mydom.com
and25
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
:NOTE: This form works well with both Postfix and Sendmail!
Example
Here I'm connecting to my mail server.
If you check the status returned by this operation:
However if nothing at the other ends accepts our connection:
Checking the status returned from this:
Putting it together
Here's my version of a script called
mail_chkr.bash
.Running it: