-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortScanner.py
More file actions
101 lines (83 loc) · 2.51 KB
/
PortScanner.py
File metadata and controls
101 lines (83 loc) · 2.51 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'''
Author: Paulo Neto
File: PortSanner.py
Description:
Scanner of ports
'''
import argparse
import socket
import pyfiglet
import os
from colorama import init , Fore, Back, Style
from threading import Thread, Lock
from queue import Queue
def cls() -> None:
os.system('cls')
# some colors
init()
GREEN = Fore.GREEN
RESET = Fore.RESET
GRAY = Fore.LIGHTBLACK_EX
CYAN = Fore.CYAN
# number of threads, fell free to tunee this parameter as you wish
N_THREADS = 200
# thread queue
q = Queue()
print_lock = Lock()
open_ports = []
def cls():
os.system('cls')
def port_scan(port):
try:
s = socket.socket()
s.connect((host, port))
except:
with print_lock:
print(f"{GRAY}{host:15}:{port:5} is closed {RESET}", end='\r')
else:
with print_lock:
print(f"{GREEN}{host:15}:{port:5} is open {RESET}")
open_ports.append(port)
finally:
s.close()
def scan_thread():
global q
while True:
# get the port number from the queue
worker = q.get()
port_scan(worker)
# tells the queue that the scanning for that port
# is done
q.task_done()
def main(host, ports):
global q
for t in range(N_THREADS):
# for each thread, start it
t = Thread(target=scan_thread)
# when we set daemon to truem that thread will end when the main thread ends
t.daemon = True
# start the daemon thread
t.start()
for worker in ports:
# for each port, put that por into the queue
# to start scanning
q.put(worker)
# wait the threeads (port scanners) to finish
q.join()
if __name__== '__main__':
# parse some parameters passed
parser = argparse.ArgumentParser(description="Simple port scanner")
parser.add_argument("host", help="Host to scan.")
parser.add_argument("--ports", "-p", dest="port_range", default=1-65535, help="Port range to scan, default is 1-65535 (all ports)")
args = parser.parse_args()
host, port_range = args.host, args.port_range
start_port, end_port = port_range.split("-")
start_port, end_port = int(start_port), int(end_port)
ports = [p for p in range(start_port, end_port)]
cls()
ascii_banner = pyfiglet.figlet_format('Port Scanner')
print(f'{CYAN}{ascii_banner}{RESET}')
print(f'\n{CYAN}Scanning {host} ...{RESET}\n')
main(host, ports)
print(f'\n{CYAN}Open ports are : {open_ports} {RESET}')
print('=======================================')