Solved

Linux script to check IMAP status

Posted on 2009-05-18
7
1,120 Views
Last Modified: 2012-05-07
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
Comment
Question by:ostraaten
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 50 total points
ID: 24415516
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
 

Author Comment

by:ostraaten
ID: 24415848
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
 
LVL 48

Expert Comment

by:Tintin
ID: 24416159
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:ostraaten
ID: 24416339
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
 

Accepted Solution

by:
ipv6ee earned 450 total points
ID: 24417471
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
 

Author Comment

by:ostraaten
ID: 24419141
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
 

Expert Comment

by:ipv6ee
ID: 24419363
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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question