-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.py
More file actions
65 lines (54 loc) · 1.75 KB
/
Copy pathScanner.py
File metadata and controls
65 lines (54 loc) · 1.75 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
import sys
import socket
from datetime import datetime
import threading
# Function to scan a port
def scan_port(target,port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
result = s.connect_ex((target,port)) #error indicator - if 0,port is open ; otherwise 1
if result == 0:
print(f"Port {port} is open")
s.close()
except socket.error as e:
print(f"Socket error on port {port}: {e}")
except Exception as e:
print(f"Unexpected error on port {port}: {e}")
# Main function - argument validation and target definition
def main():
if len(sys.argv) == 2:
target = sys.argv[1]
else:
print("Invalid usage of arguments.")
print("Usage: python.exe scanner.py <target>")
sys.exit(1)
# Resolve the target hostname to an IP address
try:
target_ip = socket.gethostbyname(target)
except socket.gaierror:
print(f"Error: Unable to resolve hostname {target}")
# Add a pretty banner
print("-" * 50)
print(f"Scanning target {target_ip}")
print(f"Time started: {datetime.now()}")
print("-" * 50)
try:
#Use multithreading to scan ports concurrently
threads = []
for port in range(1,65536):
thread = threading.Thread(target=scan_port, args=(target_ip, port))
threads.append(thread)
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
except KeyboardInterrupt:
print("\nExiting program.")
sys.exit(0)
except socket.error as e:
print(f"Socket error: {e}")
sys.exit(1)
print("\nScan completed!")
if __name__ == "__main__":
main()