Solved

Linux script to check IMAP status

Posted on 2009-05-18
7
1,008 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now