Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to use a simple 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.

share|improve this question
    
Is checking if the server is enough? Does the mailserver can crash alone? –  A.L Apr 19 at 16:00
    
Check out the code snippet in my answer here –  Bananguin Apr 19 at 16:27
    
A system management tool like chef or puppet would be a good tool to make sure it stays running. –  spuder Apr 20 at 1:59

1 Answer 1

up vote 4 down vote accepted

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
share|improve this answer
    
I think it's cleaner to send a single . to terminate the SMTP session, rather than EOF after the HELO. –  Bananguin Apr 19 at 18:56
    
@Bananguin - sure I'll add that, thanks. –  slm Apr 19 at 18:58
    
@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. –  slm Apr 19 at 19:00
    
Works great. I have added -w 5 for nc as a timeout of 5 sec. Otherwise it seems to hang indefinitely when it cannot reach the server. –  user1968963 Apr 19 at 19:40
    
@user1968963 - Yes I meant to mention that. I'll add it into the answer. –  slm Apr 19 at 19:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.