-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanports.py
More file actions
30 lines (23 loc) · 849 Bytes
/
scanports.py
File metadata and controls
30 lines (23 loc) · 849 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
import socket
from datetime import datetime
def scan_port(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1) # Timeout after 1 second
result = sock.connect_ex((ip, port)) # Returns 0 if port is open
if result == 0:
print(f"Port {port} is open")
sock.close()
# Function to scan a range of ports
def scan_ports(ip):
print(f"Scanning {ip} from port {0} to {65535}...")
# Get the start time
start_time = datetime.now()
# Iterate through the specified port range
for port in range(0, 65535 + 1):
scan_port(ip, port)
# Get the end time and calculate the duration
end_time = datetime.now()
duration = end_time - start_time
print(f"\nScan completed in {duration}.")
target_ip = input("Enter the IP address to scan: ")
scan_ports(target_ip)