forked from mdscunningham/shell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailtest.sh
executable file
·188 lines (159 loc) · 5.21 KB
/
emailtest.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
183
184
185
186
187
188
# +----+----+----+----+
# | | | | |
# Author: Mark David Scott Cunningham | M | D | S | C |
# +----+----+----+----+
# Created: 2014-02-08
# Updated: 2017-05-23
#
#
#!/bin/bash
# http://doc.coker.com.au/internet/how-to-debug-smtp-with-tlsssl-and-auth/ (SMTP-ssl)
# http://www.anta.net/misc/telnet-troubleshooting/smtp.shtml (SMTP-telnet)
# http://www.anta.net/misc/telnet-troubleshooting/imap.shtml (IMAP-ssl)
# http://www.anta.net/misc/telnet-troubleshooting/pop.shtml (POP3-ssl)
showcerts="-quiet"
tlsopts=''
secure=''
# Define help function for use later
_help(){
echo -e "\n Usage: $(basename $0) [options] [hostname] [email-address] [password]\n
-i | --imap ..... Test IMAP
-p | --pop3 ..... Test POP3
-s | --smtp ..... Test SMTP
-S | --ssl ...... Use SSL connection
-T | --tls ...... Use StartTLS connection
-P | --port ..... Set alternate port
-v | --verbose .. Show full SSL Cert output
Examples:
SMTP with TLS
$(basename $0) -s -T [host] [email] [pass]
IMAP with SSL
$(basename $0) -i -S [host] [email] [pass]
POP3 with SSL
$(basename $0) -p -S [host] [email] [pass]
SMTP with alternate port
$(basename $0) -s -P 26 [host] [email] [pass]\n"; exit 0
}
# Execute Getopt for argument parsing
OPTIONS=$(getopt -o "hipP:sSTv" --long "help,smtp,imap,pop3,ssl,tls,port:,verbose" -- "$@")
# Check for bad arguments
if [ $? -ne 0 ] ; then _help; fi
eval set -- "$OPTIONS" # Magic
while true; do # Evaluate the options for their options
case "$1" in
-h|--help) _help ;;
-i|--imap) method=imap; port=143 ;;
-p|--pop3) method=pop3; port=110 ;;
-s|--smtp) method=smtp; port=25 ;;
-S|--ssl)
case $method in
smtp) port=465 ;;
imap) port=993 ;;
pop3) port=995 ;;
esac; secure=1;
;;
-T|--tls)
case $method in
smtp) port=587; tlsopts="-starttls smtp" ;;
imap) tlsopts="-starttls imap" ;;
pop3) tlsopts="-starttls pop3" ;;
esac; secure=1;
;;
-P|--port)
if [[ $2 =~ [0-9]{2,5} ]]; then
port=$2; shift;
else
echo -e "\n The port option requires a valid parameter."
fi ;;
-v|--verbose) showcerts='' ;;
--) # More Magic
shift; break;;
*) _help ;;
esac; shift
done
# If the host, user, pass are not specified at run time prompt for them
if [[ -n $1 ]]; then
if [[ -z $1 ]]; then read -p "Hostname: " host; else host=$1; fi
if [[ -z $2 ]]; then read -p "Email Address: " emailaddr; else emailaddr=$2; fi
if [[ -z $3 ]]; then read -sp "Password: " emailpass; else emailpass=$3; fi
fi
# Check if expect is installed
if [[ ! -x $(which expect 2>/dev/null) ]]; then
echo -e "\nThis script requires the expect package.\n"
echo -e "Try one of the following:\n sudo yum install expect\n sudo apt-get install expect\n"; exit 2;
fi
# Set the connection method for ssl/tls if selected
if [[ $secure ]]; then
domain=$(echo $emailaddr | cut -d@ -f2)
# Check if local version of OpenSSL has SNI support
if [[ $(openssl version | awk '{print $2}') =~ ^1\. ]]; then SNI="-servername $domain"; else SNI=''; fi
# Check the DH-Key strength
echo; echo | openssl s_client -nbio $tlsopts $SNI -connect $host:$port -cipher "EDH" 2>/dev/null | grep "Server Temp Key";
connect="openssl s_client $tlsopts -crlf $SNI -connect $host:$port $showcerts"
else
# Check the utility to use for non-ssl connections
if [[ -x $(which nc 2>/dev/null) ]]; then
connect="nc -C $host $port"
elif [[ -x $(which ncat 2>/dev/null) ]]; then
connect="ncat -C $host $port"
elif [[ -x $(which netcat 2>/dev/null) ]]; then
connect="netcat -C $host $port"
elif [[ -x $(which telnet 2>/dev/null) ]]; then
connect="telnet $host $port"
else
echo -e "\nThis script requires telnet or netcat for insecure connections.\n"
echo -e "Try one of the following:\n sudo yum install netcat\n sudo yum install telnet\n sudo apt-get install netcat\n sudo apt-get install telnet\n"; exit 2;
fi
fi
echo
case $method in
imap )
#echo -e "\n 1) [tag] LOGIN <address> <password>\n 2) [tag] LIST \"\" \"*\"\n 3) [tag] SELECT INBOX\n 4) [tag] FETCH 1 BODY[]\n 5) [tag] LOGOUT\n"
expect -c "
spawn $connect
expect OK
send \"tag LOGIN ${emailaddr} ${emailpass}\r\"
expect OK
send \"tag SELECT INBOX\r\"
expect OK
send \"tag FETCH 1 BODY\[\]\r\"
expect completed
send \"tag LOGOUT\r\"
interact
" 2> /dev/null ;;
pop3 )
#echo -e "\n 1) USER <address>\n 2) PASS <password>\n 3) STAT\n 4) LIST\n 5) RETR 1\n 6) QUIT\n";
expect -c "
spawn $connect
expect OK
send \"USER ${emailaddr}\r\"
expect OK
send \"PASS ${emailpass}\r\"
expect OK
send \"STAT\r\"
expect OK
send \"RETR 1\r\"
expect .
send \"QUIT\r\"
interact
" 2> /dev/null ;;
smtp )
#echo -e "\n 1) HELO $host\n 2) MAIL FROM:<address>\n 3) RCPT TO:<address>\n 4) DATA\n 5) .\n 6) QUIT\n";
newuser=$(echo -n $emailaddr | base64)
newpass=$(echo -n $emailpass | base64)
expect -c "
spawn $connect
expect -re (250|220)
send \"EHLO $(hostname)\r\"
expect 250
send \"AUTH LOGIN\r\"
expect 334
send \"$newuser\r\"
expect 334
send \"$newpass\r\"
expect 235
send \"QUIT\r\"
interact
" 2> /dev/null ;;
esac
echo