/
imap-test.sh
executable file
·182 lines (151 loc) · 3.89 KB
/
imap-test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
#
# @author Gerhard Steinbeis (info [at] tinned-software [dot] net)
# @copyright Copyright (c) 2013
version=0.1.1
# @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
# @package email
#
# Configuration for the script
WAIT_TIME_LONG=2.5
WAIT_TIME=0.5
# How to mark the client and server content
CM="C:"
SM="S:"
# Initialize the variables
AUTH_ON="no-auth"
IMAP_SERVER=""
CONN_TYPE="non-ssl"
#
# Parse all parameters
#
HELP=0
while [ $# -gt 0 ]; do
case $1 in
# General parameter
-h|--help)
HELP=1
shift
;;
-v|--version)
echo
echo "Copyright (c) 2013 Tinned-Software (Gerhard Steinbeis)"
echo "License GNUv3: GNU General Public License version 3 <http://opensource.org/licenses/GPL-3.0>"
echo
echo "`basename $0` version $version"
echo
exit 0
;;
# specific parameters
auth)
AUTH_ON=$1
AUTH_USER=$2
echo -n "Enter IMAP-Password: "
read AUTH_PASS
shift 2
;;
# specific parameters
ssl)
CONN_TYPE="ssl"
shift
;;
# Unnamed parameter
*)
if [[ "$IMAP_SERVER" == "" ]]; then
IMAP_SERVER=$1
fi
shift
;;
esac
done
# Parameter check
if [[ "$IMAP_SERVER" == "" ]]; then
HELP=1
fi
# Parameter check
if [[ "$AUTH_ON" == "no-auth" ]]; then
HELP=1
fi
echo "IMAP-Server: $IMAP_SERVER"
echo "Connection: $CONN_TYPE"
if [[ "$AUTH_ON" == "auth" ]]; then
echo " Auth: YES"
echo " Auth-User: $AUTH_USER"
echo " Auth-Pass: *****"
AUTH_EXT="_auth"
else
echo " Auth: NO"
AUTH_EXT=""
fi
echo
# check the connection type
if [[ "$CONN_TYPE" == "ssl" ]]; then
SSL_EXT="_ssl"
fi
# Define extention for the logfile
LOG_EXT="${AUTH_EXT}${SSL_EXT}"
# show help message
if [ "$HELP" -eq "1" ]; then
echo
echo "Copyright (c) 2013 Tinned-Software (Gerhard Steinbeis)"
echo "License GNUv3: GNU General Public License version 3 <http://opensource.org/licenses/GPL-3.0>"
echo
echo "This script is used to test the IMAP mail-server setup. It connects to the "
echo "mail-server (optional with via SSL) and tries to check for emails."
echo "The raw communication is afterwards shown and available in the log."
echo
echo "Usage: `basename $0` [-hv] auth username imap.domain.com [ssl]"
echo " -h --help print this usage and exit"
echo " -v --version print version information and exit"
echo " auth Use authentication with username and password"
echo " ssl Connect via StartSSL to the mail server"
echo
exit 1
fi
# Generate the DATE info for the email header
DATE=`date`
echo -n "Starting the test ... "
echo -n >transaction$LOG_EXT.log
CC=1
(sleep $WAIT_TIME_LONG
echo "$CM a$CC LOGIN $AUTH_USER $AUTH_PASS" >>transaction$LOG_EXT.log &&
echo "a$CC LOGIN $AUTH_USER $AUTH_PASS"
CC=`expr $CC + 1`
sleep $WAIT_TIME
echo "$CM a$CC LIST \"\" \"*\"" >>transaction$LOG_EXT.log &&
echo "a$CC LIST \"\" \"*\""
CC=`expr $CC + 1`
sleep $WAIT_TIME
echo "$CM a$CC EXAMINE INBOX" >>transaction$LOG_EXT.log &&
echo "a$CC EXAMINE INBOX"
CC=`expr $CC + 1`
sleep $WAIT_TIME
echo "$CM a$CC SELECT INBOX" >>transaction$LOG_EXT.log &&
echo "a$CC SELECT INBOX"
CC=`expr $CC + 1`
sleep $WAIT_TIME
echo "$CM a$CC FETCH 1 BODY[]" >>transaction$LOG_EXT.log &&
echo "a$CC FETCH 1 BODY[]"
CC=`expr $CC + 1`
sleep $WAIT_TIME
echo "$CM a$CC LOGOUT" >>transaction$LOG_EXT.log &&
echo "a$CC LOGOUT"
) |
if [[ "$CONN_TYPE" == "ssl" ]]; then
openssl s_client -connect $IMAP_SERVER:993 >>transaction$LOG_EXT.log 2>&1
else
telnet $IMAP_SERVER 143 >>transaction$LOG_EXT.log 2>&1
fi
echo "FINISHED"
echo
echo
TRANSACTION=`cat transaction$LOG_EXT.log | sed -E "/$CM /! s/^(.*)$/$SM &/"`
echo -n >transaction$LOG_EXT.log
if [[ "$CONN_TYPE" == "ssl" ]]; then
echo "openssl s_client -connect $IMAP_SERVER:993" >>transaction$LOG_EXT.log
else
echo "telnet $IMAP_SERVER 143" >>transaction$LOG_EXT.log
fi
echo "$TRANSACTION" >>transaction$LOG_EXT.log
cat transaction$LOG_EXT.log
echo