-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscanner.py
48 lines (40 loc) · 963 Bytes
/
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
#!/usr/bin/env python3
# tcp port scan example with multi-threading
import threading
import socket
import sys
from queue import Queue
print_lock = threading.Lock()
q = Queue()
host = "10.10.15.4"
# tcp scan current port
def tcp_scan(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
res = sock.connect_ex((host, port))
if res == 0:
print ('port {}: open'.format(port))
sock.close()
except socket.error:
print ("failed connect to server :(")
sys.exit()
except KeyBoardInterrupt:
print ("exit...")
sys.exit()
# run scan
def run():
while True:
p = q.get()
tcp_scan(p)
q.task_done()
# main logic
def main():
for p in range(1000):
thread = threading.Thread(target = run)
thread.daemon = True
thread.start()
for p in range(1000):
q.put(p)
q.join()
if __name__ == "__main__":
main()