simple script for monitoring a mailserver
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
email monitoring postfix
add a comment |
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.
email monitoring postfix
Is checking if the server is enough? Does the mailserver can crash alone?
– A.L
Apr 19 '14 at 16:00
Check out the code snippet in my answer here
– Bananguin
Apr 19 '14 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 '14 at 1:59
add a comment |
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.
email monitoring postfix
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.
email monitoring postfix
email monitoring postfix
edited Mar 18 at 2:04
Rui F Ribeiro
42.1k1484142
42.1k1484142
asked Apr 19 '14 at 15:41
user1968963user1968963
1,026102646
1,026102646
Is checking if the server is enough? Does the mailserver can crash alone?
– A.L
Apr 19 '14 at 16:00
Check out the code snippet in my answer here
– Bananguin
Apr 19 '14 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 '14 at 1:59
add a comment |
Is checking if the server is enough? Does the mailserver can crash alone?
– A.L
Apr 19 '14 at 16:00
Check out the code snippet in my answer here
– Bananguin
Apr 19 '14 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 '14 at 1:59
Is checking if the server is enough? Does the mailserver can crash alone?
– A.L
Apr 19 '14 at 16:00
Is checking if the server is enough? Does the mailserver can crash alone?
– A.L
Apr 19 '14 at 16:00
Check out the code snippet in my answer here
– Bananguin
Apr 19 '14 at 16:27
Check out the code snippet in my answer here
– Bananguin
Apr 19 '14 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 '14 at 1:59
A system management tool like chef or puppet would be a good tool to make sure it stays running.
– spuder
Apr 20 '14 at 1:59
add a comment |
1 Answer
1
active
oldest
votes
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
I think it's cleaner to send a single.
to terminate the SMTP session, rather thanEOF
after theHELO
.
– Bananguin
Apr 19 '14 at 18:56
@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 '14 at 19:00
Works 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.
– user1968963
Apr 19 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@slm - on the mailserver, I see these messages in the log:postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?
– user1968963
Apr 19 '14 at 19:51
|
show 8 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f125557%2fsimple-script-for-monitoring-a-mailserver%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
I think it's cleaner to send a single.
to terminate the SMTP session, rather thanEOF
after theHELO
.
– Bananguin
Apr 19 '14 at 18:56
@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 '14 at 19:00
Works 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.
– user1968963
Apr 19 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@slm - on the mailserver, I see these messages in the log:postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?
– user1968963
Apr 19 '14 at 19:51
|
show 8 more comments
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
I think it's cleaner to send a single.
to terminate the SMTP session, rather thanEOF
after theHELO
.
– Bananguin
Apr 19 '14 at 18:56
@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 '14 at 19:00
Works 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.
– user1968963
Apr 19 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@slm - on the mailserver, I see these messages in the log:postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?
– user1968963
Apr 19 '14 at 19:51
|
show 8 more comments
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
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
edited Apr 19 '14 at 20:38
answered Apr 19 '14 at 18:38
slm♦slm
256k71544690
256k71544690
I think it's cleaner to send a single.
to terminate the SMTP session, rather thanEOF
after theHELO
.
– Bananguin
Apr 19 '14 at 18:56
@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 '14 at 19:00
Works 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.
– user1968963
Apr 19 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@slm - on the mailserver, I see these messages in the log:postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?
– user1968963
Apr 19 '14 at 19:51
|
show 8 more comments
I think it's cleaner to send a single.
to terminate the SMTP session, rather thanEOF
after theHELO
.
– Bananguin
Apr 19 '14 at 18:56
@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 '14 at 19:00
Works 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.
– user1968963
Apr 19 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@slm - on the mailserver, I see these messages in the log:postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?
– user1968963
Apr 19 '14 at 19:51
I think it's cleaner to send a single
.
to terminate the SMTP session, rather than EOF
after the HELO
.– Bananguin
Apr 19 '14 at 18:56
I think it's cleaner to send a single
.
to terminate the SMTP session, rather than EOF
after the HELO
.– Bananguin
Apr 19 '14 at 18:56
@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 '14 at 19:00
@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 '14 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 '14 at 19:40
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 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@user1968963 - Yes I meant to mention that. I'll add it into the answer.
– slm♦
Apr 19 '14 at 19:40
@slm - on the mailserver, I see these messages in the log:
postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?– user1968963
Apr 19 '14 at 19:51
@slm - on the mailserver, I see these messages in the log:
postfix/smtpd improper command pipelining after HELO from unknown: QUITn
. Can we get rid of them, or is postfix just complaining that I hang up prematurely?– user1968963
Apr 19 '14 at 19:51
|
show 8 more comments
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f125557%2fsimple-script-for-monitoring-a-mailserver%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is checking if the server is enough? Does the mailserver can crash alone?
– A.L
Apr 19 '14 at 16:00
Check out the code snippet in my answer here
– Bananguin
Apr 19 '14 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 '14 at 1:59