-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_scan.py
More file actions
36 lines (27 loc) · 975 Bytes
/
port_scan.py
File metadata and controls
36 lines (27 loc) · 975 Bytes
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
#Python 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] on linux
#Type "help", "copyright", "credits" or "license()" for more information.
import socket
import random
def port_scanner(target, ports):
open_ports = []
for port in ports:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex((target, port))
if result == 0:
open_ports.append(port)
sock.close()
return open_ports
def generate_random_ports(n):
return random.sample(range(1, 65536), n)
if __name__ == "__main__":
target = input("Enter the target IP address or hostname: ")
ports = generate_random_ports(3498)
print(f"Scanning {len(ports)} ports on {target}...")
open_ports = port_scanner(target, ports)
if open_ports:
print("Open ports:")
for port in open_ports:
print(port)
else:
print("No open ports found.")