forked from adviksinghania/ethical-hacking-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaded_port_scanner.py
More file actions
executable file
·57 lines (45 loc) · 1.63 KB
/
Copy paththreaded_port_scanner.py
File metadata and controls
executable file
·57 lines (45 loc) · 1.63 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
#!/usr/bin/env python3
# threaded_port_scanner.py
"""Threaded Port Scanner using Python3"""
import argparse
import sys
import socket
import time
import concurrent.futures
from datetime import datetime
def set_arguments(p):
# Setting options for command line arguments
p.add_argument('-i', '--ip', dest='ip', help='IPv4 address to scan the ports')
def get_arguments(p):
# Parsing the command line arguments and returning the arguments
arguments = p.parse_args()
if arguments.ip is None:
p.error(f'[!] Please specify an IP address. Type {sys.argv[0]} -h for more info.')
else:
return arguments
def scan_port(target, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
# returns an error indicator
result = s.connect_ex((target, port))
s.close()
if result == 0:
print(f'[+] Port {port} is open.')
if __name__ == '__main__':
parser = argparse.ArgumentParser() # ArgumentParser() class to parse the command line arguments
set_arguments(parser)
opt = get_arguments(parser)
print(f'[!] Port Scanning started at {str(datetime.now())} for target IP {opt.ip}.')
start = time.time()
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=128) as tpe:
for port in range(1, 65535):
tpe.submit(scan_port, opt.ip, port)
stop = time.time()
print(f'Scan done in {round(stop - start, 5)}s.')
except socket.gaierror:
print("\n[!] Hostname could not be resolved.")
except socket.error:
print("\n[!] Server not responding.")
finally:
sys.exit(0)