forked from Oros42/checkemailexists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkemailexists.sh
executable file
·126 lines (123 loc) · 2.33 KB
/
checkemailexists.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
#!/usr/bin/env bash
# author : Oros
# 2016-05-30
#
# Check if an email address exists
# ./checkemailexists.sh <mail_to_check>
if [ ! `which expect` ]; then
echo -e "\033[31mNeed expect!\033[0m\nsudo apt install expect"
exit 1
fi
if [ ! `which nslookup` ]; then
echo -e "\033[31mNeed nslookup!\033[0m\nsudo apt install dnsutils"
exit 1
fi
if [ ! `which telnet` ]; then
echo -e "\033[31mNeed telnet!\033[0m\nsudo apt install telnet"
exit 1
fi
if [ $# -ne 1 ]; then
echo "$0 <mail_to_check>"
exit 1
fi
themail="$1"
domain=${themail##*@}
mailHost=`nslookup -type=mx $domain |grep exchanger | head -n 1`
if [ "$mailHost" == "" ]; then
echo -e "\033[31mNo email server at this domain : $domain\033[0m"
exit 1
fi
mailHost=${mailHost##* }
if [ "$mailHost" == "" ]; then
echo -e "\033[31mError 1 with the domain\033[0m"
exit 1
fi
mailHost=${mailHost::-1}
if [ "$mailHost" == "" ]; then
echo -e "\033[31mError 2 with the domain\033[0m"
exit 1
fi
a=$(
/usr/bin/expect -c '
proc connect {} {
expect {
"220" {
#send_user "Send HELO\n"
send "HELO '$domain'\r"
sleep 0.5
expect {
"250" {
send "mail from:<mr.nobody.404@gmail.com>\r"
sleep 0.5
expect {
"250" {
send "rcpt to:<'$themail'>\r"
sleep 0.5
expect {
"250" {
send_user "'$themail' Ok\n"
sleep 0.5
send "quit\r"
return 1
}
"Protocol error" {
send "quit\r"
return 3
}
"550" {
#send_user "'$themail' mailbox unavailable\n"
send "quit\r"
return 2
}
"501" {
#send_user "'$themail' Invalid Address\n"
send "quit\r"
return 2
}
incorrect {
#send_user "'$themail' NO\n"
send "quit\r"
return 2
}
}
}
"501" {
#send_user "Oups Invalid Address for sender\n"
send "quit\r"
return 4
}
}
}
}
}
}
return 5
}
spawn telnet '$mailHost' 25
sleep 0.5
set rez [connect]
#sleep 0.5
exit $rez
'
)
ex=$?
case $ex in
1)
echo "Ok"
exit 0;;
2)
echo "mailbox unavailable"
exit 2;;
3)
echo "Protocol error. Try it again"
exit 3;;
4)
echo "Oups Invalid Address for sender"
exit 4;;
5)
echo "Error 0_o. Not 220 when connect to $mailHost"
exit 5;;
*)
echo "Error 0_o ($ex)"
exit 6;;
esac