-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmail
executable file
·166 lines (154 loc) · 5.2 KB
/
cmail
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
#!/bin/bash
# Check if commands exist
mail_command_ne=""
if ! type -p dig >& /dev/null || [ -z "$(type -p dig 2> /dev/null)" ]; then
mail_command_ne+="Command "dig" not found. Please install "dig" before running this script.\n"
fi
if ! type -p nc >& /dev/null || [ -z "$(type -p nc 2> /dev/null)" ]; then
mail_command_ne+="Command "nc" not found. Please install "netcat" before running this script.\n"
fi
if ! type -p expect >& /dev/null || [ -z "$(type -p expect 2> /dev/null)" ]; then
mail_command_ne+="Command "expect" not found. Please install "expect" before running this script.\n"
fi
if ! type -p telnet >& /dev/null || [ -z "$(type -p telnet 2> /dev/null)" ]; then
mail_command_ne+="Command "telnet" not found. Please install "telnet" before running this script."
fi
if [ -n "$mail_command_ne" ]; then
echo -e "$mail_command_ne"
exit 1
fi
# smpt ports
mail_ports=(25 587 465 2525)
mail_verbose=0
# Print help menu
if [ "$1" = "--help" ]; then
echo "Usage: $0 [-d <email.domain>] [-e <smtp-server>] [-p <port>] mail@address"
echo " -d: specifies the email domain"
echo " -e: specifies the smtp server domain (mail exchanger)"
echo " -p: specifies the smtp server port"
echo " -v: verbose. Prints detailed information"
exit 0
fi
# Get Options
while getopts "hd:e:p:v" OPTION
do
case $OPTION in
h)
echo "Usage: $0 [-d <email.domain>] [-e <smtp-server>] [-p <port>] mail@address"
echo " -d: specifies the email domain"
echo " -e: specifies the smtp server domain (mail exchanger)"
echo " -p: specifies the smtp server port"
exit 0
;;
d)
mail_domain=$OPTARG
;;
e)
if ping -c1 -w1 $OPTARG >& /dev/null; then
mail_exchanger=( $OPTARG )
else
echo "$OPTARG is not reachable."
exit 1
fi
;;
p) mail_port=( $OPTARG )
;;
v) mail_verbose=1
;;
\?)
echo "Usage: $0 [-d <email.domain>] [-e <smtp-server>] [-p <port>] mail@address"
echo " -d: specifies the email domain"
echo " -e: specifies the smtp server domain (mail exchanger)"
echo " -p: specifies the smtp server port"
exit 1
;;
esac
done
shift $(($OPTIND-1))
# Check if email adress format is valid
if [ "$#" -lt 1 ]; then
echo "Usage: $0 [-d <email.domain>] [-e <smtp-server>] [-p <port>] mail@address"
echo " -d: specifies the email domain"
echo " -e: specifies the smtp server domain (mail exchanger)"
echo " -p: specifies the smtp server port"
exit 1
elif echo "$1" | grep "^[a-zA-Z0-9._%+-]*@[a-zA-Z0-9._-]*\.[a-zA-Z0-9]*$" > /dev/null; then
mail_address=$1
else
echo "E-mail address not valid."
exit 1
fi
# If no mail domain is specified, use the one from the email adress
if [ -z "$mail_domain" ]; then
mail_domain=${mail_address##*@}
fi
# Get the mail exchanger belonging to the mail domain, if not specified
if [ ${#mail_exchanger[@]} -eq 0 ]; then
mail_exchanger=( $(dig +short $mail_domain mx | tr -s ' ' | cut -d ' ' -f2) )
for ((i=0; i<${#mail_exchanger[@]};i++)); do
mail_exchanger[$i]=${mail_exchanger[$i]%?}
done
fi
# Verbose option
if [ $mail_verbose -gt 0 ]; then
echo "mail address: $mail_address"
echo "mail domain: $mail_domain"
echo "mail exchanger: ${mail_exchanger[0]}"
fi
# Check if email address is valid
if [ ${#mail_exchanger[@]} -ne 0 ]; then
mail_ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
mail_hostname=$(dig +short -x $mail_ip)
mail_hostname=${mail_hostname%?}
if [ $mail_verbose -gt 0 ]; then
echo "ip address: $mail_ip"
echo "hostname (public): $mail_hostname"
fi
for i in ${mail_ports[@]}; do
if [ $mail_verbose -gt 0 ]; then
echo -n "port $i:"
fi
if nc -zv -w 1 ${mail_exchanger[0]} $i 2>&1 | grep "(smtp)" > /dev/null; then
if [ $mail_verbose -gt 0 ]; then
echo " open"
fi
mail_test="`expect << __EOF
#! /usr/bin/env expect
set timeout 1
spawn telnet ${mail_exchanger[0]} $i
expect "220"
send "HELO $mail_hostname\r"
expect "250"
send "MAIL FROM: <$mail_address>\r"
expect "250"
send "RCPT TO: <$mail_address>\r"
expect "*"
send "QUIT\r"
expect eof
__EOF`"
if [ $mail_verbose -gt 0 ]; then
echo
echo "$mail_test"
echo
fi
if [ "$(echo "$mail_test" | tail -n -3 | head -n 1 | cut -d ' ' -f1)" = "250" ]; then
echo "E-Mail is valid."
exit 0
else
echo "E-Mail is not valid."
exit 1
fi
else
if [ $mail_verbose -gt 0 ]; then
echo " closed or not smtp"
fi
fi
done
echo "Tested port(s) ${mail_ports[@]} without success."
echo "Try $0 -p <smpt-port> $1"
exit 1
else
echo "There's something wrong with the e-mail domain: Couldn't find mail exchanger."
echo "Try $0 -d <email.domain> $1"
exit 1
fi