-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortScanner.py
More file actions
68 lines (55 loc) · 2.3 KB
/
PortScanner.py
File metadata and controls
68 lines (55 loc) · 2.3 KB
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
import socket
import threading
import queue
NUM_THREADS = 50 # Number of threads for concurrent scanning
OUTPUT_FILE = "open_ports.txt" # Output file to store information about open ports
def port_scan(target_host, port, open_ports):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Set socket timeout to 1 second
result = sock.connect_ex((target_host, port))
if result == 0:
print(f"Port {port}: Open")
open_ports.append(port) # Collect open port numbers
sock.close()
except socket.error:
pass # Handle socket errors gracefully (e.g., connection refused)
def get_device_info(target_host):
try:
host_name = socket.gethostbyaddr(target_host)[0]
return host_name
except socket.herror:
return "(Unknown)"
def worker(open_ports):
while True:
port = port_queue.get()
port_scan(target_host, port, open_ports)
port_queue.task_done()
if __name__ == "__main__":
target_host = input("Enter the target host IP address or domain name: ")
start_port = int(input("Enter the starting port number: "))
end_port = int(input("Enter the ending port number: "))
# Display device info if input is an IP address
if target_host.replace('.', '').isdigit(): # Check if input is an IP address
device_name = get_device_info(target_host)
print(f"Device Name: {device_name}")
print(f"IP Address: {target_host}")
open_ports = []
port_queue = queue.Queue()
# Create worker threads
for _ in range(NUM_THREADS):
t = threading.Thread(target=worker, args=(open_ports,))
t.daemon = True
t.start()
# Enqueue ports to be scanned
for port in range(start_port, end_port + 1):
port_queue.put(port)
# Wait for all tasks to be completed
port_queue.join()
# Write information about open ports to output file
with open(OUTPUT_FILE, "w") as file:
file.write(f"Device Name: {device_name}\n")
file.write(f"IP Address: {target_host}\n")
file.write(f"Open Ports: {' '.join(map(str, open_ports))}\n")
print(f"Open ports information written to '{OUTPUT_FILE}'")
# created and edited by Adityasinh.