This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtp-check.py
83 lines (75 loc) · 2.14 KB
/
smtp-check.py
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
#!/usr/bin/python
#we will use cmd style of our script - just for practice in python and for usability
import sys,getopt,socket,fileinput
#split cmd strings and filelines in one list
def get_data(data,datafile):
result = []
if datafile:
with open(datafile) as f:
result = f.readlines()
result = [x.strip('\n') for x in result]
if data:
result.append(data)
return result
#function "user_verify" that try to verify all users from file or cmd at one separate ip
#need to do this function parallel
def user_verify(ip,port,userlist):
result = []
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip,port))
s.recv(1024)
except:
print 'Oops can\'t connect to '+ip+' with port '+str(port)
for user in userlist:
s.send('VRFY '+user+'\r\n')
res = s.recv(1024)
#print res
if "250" in res:
result.append(user)
##I need only registered users ;-)
#elif "252" in res:
# result.append(user)
s.close()
return result
#function "check_ip" calls function user_verify on all server ips from file or cmd
#try to do it parallel
def check_ip(ips,port,userlists):
result = []
for ip in ips:
users = user_verify(ip,port,userlists)
if users:
result.append([ip,users])
return result
def main(argv):
server = ''
serverlist = ''
port = 25
username = ''
usernamelist = ''
try:
opts, args = getopt.getopt(argv,"hs:S:p:u:U:",["server=","Serverlist=","port=","username=","Usernamelist="])
except getopt.GetoptError:
print 'smtp-check.py -s <server> -S <Serverlist> -p <port> -u <username> -U <Usernamelist>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'smtp-check.py -s <server> -S <Serverlist> -p <port> -u <username> -U <Usernamelist>'
sys.exit(1)
if opt in ('-s','--server'):
server = arg
if opt in ('-S','--Serverlist'):
serverlist = arg
if opt in ('-p','--port'):
port = int(arg)
if opt in ('-u','--username'):
username = arg
if opt in ('-U','--Usernamelist'):
usernamelist = arg
#main process here
userlist = get_data(username,usernamelist)
iplist = get_data(server,serverlist)
check = check_ip(iplist,port,userlist)
print check
if __name__ == "__main__":
main(sys.argv[1:])