-
Notifications
You must be signed in to change notification settings - Fork 3
/
sock_scanner.py
51 lines (38 loc) · 1.03 KB
/
sock_scanner.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
import socket
import threading
def TCP_connect(ip, port, delay, key, output):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(delay)
try:
sock.connect((ip,port))
output[key] = 'up'
except:
pass
sock.close()
#Expects list of Name,ip:port
def scan_ips(data):
delay=5
threads = []
output = {}
for rec in data.split('\n'):
if rec == '' or rec[0] == '#':
continue
temp = rec
[ip,port] = temp.split(':')
t = threading.Thread(target=TCP_connect, args=(ip,int(port), delay, rec, output))
threads.append(t)
for i in range(len(threads)/100):
num_threads_left = len(threads)-i*100
num_threads = 100
if num_threads_left < 100:
num_threads = num_threads_left
for j in range(i*100,i*100+num_threads):
threads[j].start()
for j in range(i*100,i*100+num_threads):
threads[j].join()
# for i in range(len(threads)):
# threads[i].start()
# for i in range(len(threads)):
# threads[i].join()
return('\n'.join(output.keys()))