Skip to content

Commit 4e4b861

Browse files
committed
make requested changes
1 parent 7aa80e7 commit 4e4b861

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

Python/Multithreaded-Port-Scanner/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
This script scans for open ports using multiple threads in parallel in which each port is checked on a separate thread. It is faster than the usual single threaded application.
33

44
## Executing the script
5-
* Run `python scan-ports.py <IP or website name> <optional ports>`
5+
* Run `python scan-ports.py <IP or website name>`
66
* for e.g the following are valid calls:
77
* `python scan-ports.py 142.250.67.142`
88
* `python scan-ports.py www.google.com`
9-
* `python scan-ports.py 142.250.67.142 34 54 80 443`
109

1110
## Output
1211

Python/Multithreaded-Port-Scanner/scan-ports.py

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,67 @@ def scan_port(target_IP, port):
4343
print(f"Port {port}: \033[92mOPEN\033[0m")
4444
return port
4545

46-
print(f"Port {port}: \033[91mCLOSED\033[0m")
4746
return None
4847

49-
# List to hold open ports.
50-
open_ports = []
5148

52-
# Create a context manager to spawn individual threads for each port.
53-
with concurrent.futures.ThreadPoolExecutor() as executor:
49+
def scan_ports(port_range):
50+
51+
if not port_range:
52+
port_range = map(int, input("Enter ports(space separated):").split())
53+
54+
# List to hold open ports.
55+
open_ports = []
56+
57+
# Create a context manager to spawn individual threads for each port.
58+
with concurrent.futures.ThreadPoolExecutor() as executor:
5459

55-
# Check if the users has provided custom ports or not.
56-
if len(custom_ports) == 0:
57-
future_to_port = {
58-
executor.submit(scan_port, target_IP, port): port for port in range(65535)
59-
}
60-
else:
6160
future_to_port = {
62-
executor.submit(scan_port, target_IP, int(port)): port for port in custom_ports
61+
executor.submit(scan_port, target_IP, port): port for port in port_range
6362
}
6463

65-
# Run a loop to collect open ports.
66-
for future in concurrent.futures.as_completed(future_to_port):
67-
# Get the selected port
68-
port = future_to_port[future]
69-
try:
70-
# Check if it a valid port.
71-
open_port = future.result()
72-
except Exception as e:
73-
# Print any errors.
74-
print(f"Exception:{e}")
75-
else:
76-
# Add to the list if it's a valid port.
77-
if open_port is not None:
78-
open_ports.append(open_port)
79-
80-
# Finally print all the open ports.
81-
print(f"Open Ports:{open_ports}")
64+
# Run a loop to collect open ports.
65+
for future in concurrent.futures.as_completed(future_to_port):
66+
# Get the selected port
67+
port = future_to_port[future]
68+
try:
69+
# Check if it a valid port.
70+
open_port = future.result()
71+
except Exception as e:
72+
# Print any errors.
73+
print(f"Exception:{e}")
74+
else:
75+
# Add to the list if it's a valid port.
76+
if open_port is not None:
77+
open_ports.append(open_port)
78+
79+
return open_ports
80+
81+
82+
if __name__ == "__main__":
83+
84+
MENU_PROMPT =\
85+
"""[1] Reserved Ports.
86+
[2] All Ports.
87+
[3] Critical Ports.
88+
[4] Manual Ports.
89+
Enter your choice:"""
90+
91+
# Get user choice
92+
choice = int(input(MENU_PROMPT))
93+
94+
# Define switcher
95+
switcher = {
96+
1: range(1024),
97+
2: range(65536),
98+
3: [15, 20, 21, 22, 23, 25, 49, 50, 51, 53, 67, 68, 69, 79, 80, 88,
99+
110, 111, 119, 123, 135, 137, 138, 139, 143, 161, 389, 443, 445,
100+
500, 520, 546, 547, 636, 993, 995, 1512, 1701, 1720, 1723, 1812,
101+
1813, 3306, 3389, 5004, 5005, 5060, 5061, 5900, 8080],
102+
4: []
103+
}
104+
105+
# Call method to scan ports.
106+
open_ports = scan_ports(switcher[choice])
107+
108+
# Finally print all the open ports.
109+
print(f"Open Ports:{open_ports}")

0 commit comments

Comments
 (0)