-
Notifications
You must be signed in to change notification settings - Fork 0
/
port-scanner.py
84 lines (61 loc) · 2.22 KB
/
port-scanner.py
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
74
75
76
77
78
79
80
81
82
83
84
"""
Multithreaded port scanner: re-implementation of http://www.pythonforbeginners.com/ Tutorial, and using techniques
from another tutorial https://code.tutsplus.com/articles/introduction-to-parallel-and-concurrent-programming-in-python--cms-28612
"""
import socket, sys, os, subprocess
from datetime import datetime
from queue import Queue
from threading import Thread
# Detetcts the number of cores
NUM_WORKERS = os.cpu_count()
task_queue = Queue()
# Clear the screen
subprocess.call('clear', shell=True)
# Ask for input
remoteServer = input("Enter a remote host to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)
port_list = [port for port in range(1, 51)]
def check_port(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print("Port {}: Open".format(port))
sock.close()
def worker():
while True:
port = task_queue.get()
check_port(port)
task_queue.task_done()
if task_queue.empty():
break
def main():
# Print a nice banner with information on which host we are about to scan
print("-" * 60)
print("Please wait, scanning remote host", remoteServerIP)
print("-" * 60)
# Check what time the scan started
t1 = datetime.now()
# Using the range function to specify ports (here it will scans all ports between 1 and 1024)
# We also put in some error handling for catching errors
try:
threads = [Thread(target=worker) for _ in range(NUM_WORKERS)]
[task_queue.put(port) for port in port_list]
[thread.start() for thread in threads]
task_queue.join()
except KeyboardInterrupt:
print("\nYou pressed Ctrl+C")
sys.exit()
except socket.gaierror:
print('Hostname could not be resolved. Exiting')
sys.exit()
except socket.error:
print("Couldn't connect to server")
sys.exit()
# Checking the time again
t2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1
# Printing the information to screen
print('Scanning Completed in: ', total)
if __name__ == '__main__':
main()