-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
73 lines (61 loc) · 2.75 KB
/
scanner.py
File metadata and controls
73 lines (61 loc) · 2.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
66
67
68
69
70
71
72
73
import sys
import socket
from datetime import datetime
import threading
#Function to scan a port
def scan_port(target,port):
try:
# Sockets - can be used to connect two nodes
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 else not
if result == 0: #if port is open, lets user know, then shuts that port
print(f"Port {port} is open")
s.close() #close to try next port
except socket.error as e:
print(f"Socket error on port {port}: {e}") #Socket error: could be anything - DNS issue, connection refused, timed out
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: #if argument (comment below ex.) is length 2 (pyth.. scan.. 192.128...)
target = sys.argv[1] #then pull target (IP address) example below
else:
print("Invalid number of arguments.")
print("Usage: python.exe scanner.py <target>")
sys.exit(1) #should be sys.exit(1)
#python.exe scanner.py 192.128.1.1 (example executable)
# Resolve the target hostname to an IP address
try:
target_ip = socket.gethostbyname(target) #if hostname provided instead of IP, converts it to IP
except socket.gaierror:
print(f"Error: Unable to resolve hostname {target}")
sys.exit(1)
# 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 (same time instead of one by one)
threads = []
for port in range(1, 65536):
#thread - seperate flow of execution that runs concurrently with other programs
thread = threading.Thread(target=scan_port, args=(target_ip, port)) #want to use function scan_port with those arguments
threads.append(thread) #after running it gets rid of that thread in the threads list
thread.start() #actually executes thread
# Wait for all threads to complete
for thread in threads:
thread.join()
except KeyboardInterrupt: #ctrl c
print("\nExiting program.")
sys.exit(0)
except socket.error as e: #when socket error occurs = an issue establishing, maintaining, or closing nodes connection
print(f"Socket error: {e}")
sys.exit(1)
#if everything runs through and scan is finished - print
print("\nScan completed!")
#Can only be executed here on its own (cant be imported) name is main
if __name__ == "__main__":
main()
#run file [IP address] - in terminal