-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport-scanner.py
58 lines (42 loc) · 1.09 KB
/
port-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
49
50
51
52
53
54
55
56
57
58
"""
port-scanner.py
> Scans all the open port for a given target
> Implements multi-threading to make script run faster, can change 'num_threads' variable
Parv Patel
"""
import threading
from queue import Queue
import socket
# target ip (uses authorized ip!)
target = "10.0.0.1"
queue = Queue()
open_ports = []
def portscan(port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((target, port))
return True
except:
return False
def fill_queue(port_list):
for port in port_list:
queue.put(port)
def worker():
while not queue.empty():
port = queue.get()
if portscan(port):
print(f'Port {port} is open!')
open_ports.append(port)
# fill queue
port_list = range(1, 1025)
fill_queue(port_list)
# TODO: name 'num_threads' command line variable
num_threads = 100
thread_list = []
for t in range(num_threads):
thread = threading.Thread(target=worker)
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
print(f'Open ports are: {open_ports}')