• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1426
  • Last Modified:

Linux script to check IMAP status

Hi,
I'm looking for a bash script (example) that executes some commands depending on if a certain (IMAP) server is on-line.

What is the best way to do this?

For example: ping the server and depending on the response execute some commands.
Thank in advance.
Onno
0
ostraaten
Asked:
ostraaten
  • 3
  • 2
  • 2
2 Solutions
 
TintinCommented:
I'd use the Nagios IMAP check as it is very comprehensive.

# ./check_imap -h
check_imap v1991 (nagios-plugins 1.4.13)
Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
Copyright (c) 1999-2008 Nagios Plugin Development Team
        <nagiosplug-devel@lists.sourceforge.net>

This plugin tests IMAP connections with the specified host (or unix socket).

Usage:check_imap -H host -p port [-w <warning time>] [-c <critical time>] [-s <send string>]
[-e <expect string>] [-q <quit string>][-m <maximum bytes>] [-d <delay>]
[-t <timeout seconds>] [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]
[-D <days to cert expiry>] [-S <use SSL>] [-E]

Options:
 -h, --help
    Print detailed help screen
 -V, --version
    Print version information
 -H, --hostname=ADDRESS
    Host name, IP Address, or unix socket (must be an absolute path)
 -p, --port=INTEGER
    Port number (default: none)
 -4, --use-ipv4
    Use IPv4 connection
 -6, --use-ipv6
    Use IPv6 connection
 -E, --escape
    Can use \n, \r, \t or \ in send or quit string. Must come before send or quit option
    Default: nothing added to send, \r\n added to end of quit
 -s, --send=STRING
    String to send to the server
 -e, --expect=STRING
    String to expect in server response (may be repeated)
 -A, --all
    All expect strings need to occur in server response. Default is any
 -q, --quit=STRING
    String to send server to initiate a clean close of the connection
 -r, --refuse=ok|warn|crit
    Accept tcp refusals with states ok, warn, crit (default: crit)
 -M, --mismatch=ok|warn|crit
    Accept expected string mismatches with states ok, warn, crit (default: warn)
 -j, --jail
    Hide output from TCP socket
 -m, --maxbytes=INTEGER
    Close connection once more than this number of bytes are received
 -d, --delay=INTEGER
    Seconds to wait between sending string and polling for response
 -D, --certificate=INTEGER
    Minimum number of days a certificate has to be valid.
 -S, --ssl
    Use SSL for the connection.
 -w, --warning=DOUBLE
    Response time to result in warning status (seconds)
 -c, --critical=DOUBLE
    Response time to result in critical status (seconds)
 -t, --timeout=INTEGER
    Seconds before connection times out (default: 10)
 -v, --verbose
    Show details for command-line debugging (Nagios may truncate output)
0
 
ostraatenAuthor Commented:
That is too advanced a solution for me, this is just to automate some stuff on my laptop.

A script that just checks if a server can be reached and if it can reach the server it should execute some commands.

0
 
TintinCommented:
Checking whether a particular port is responding or not is straight forward, but once you add in the requirement to execute commands and check the response/status, then it becomes  a lot more complicated.

Do you have a specific example of the IMAP commands you want to run?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ostraatenAuthor Commented:
No not at this point.

I think a simple ping can suffice, if the server can be reached it should run a script, it can be any script.
0
 
ipv6eeCommented:
Hi,

i have attached  my script that i use for HTTP and others i modified it for IMAP server and it will mail the status of imap server to u(root) if its running or not.
You can chage port according to urs need i.e 993 to 995 in the script
You can add more services status like ntp everthing will be same.

Cheers
#!/bin/bash
 
 
###### IMAPs SECTION ###########
netstat -ntulp | grep 993 > /dev/null 
 
IMAPs="$?"
 
if [ $IMAPs -eq  0 ];then
 
echo 	"On $(date '+%F at %r') IMAPs is up and listening" | mail -s "IMAPs server status" root
 
else 
 
echo "On $(date '+%F %r') IMAP is not running" | mail -s "IMAPs server is down and needs yours attention immediatly" root
 
 
 
fi

Open in new window

0
 
ostraatenAuthor Commented:
The script turned out to be not exactly what I needed as it tests for a local IMAP server, I want to check if a remote (IMAP) server can be reached. Basically I want to check if I'm connected to the network.  

But I could change the script a bit easily to my needs by using a ping command.
#!/bin/bash
 
ping -c 1 myimapserver | grep "1 received" > /dev/null
 
RESULT="$?"
 
if [ $RESULT -eq  0 ];then
 
echo    "On $(date '+%F at %r') server found" 
 
else 
 
echo "On $(date '+%F %r') server is not found" 
 
 
fi

Open in new window

0
 
ipv6eeCommented:
if u put IMAP script in crontab suppose to run every 10 minutes it will mail to the email-id u specify the status of IMAP every 10 minutes so it will work on remotely too :-)
 nd same thing u can do with urs own script too.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now