-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvancedportscanner.py
88 lines (70 loc) · 2.56 KB
/
advancedportscanner.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
85
86
87
88
#!/usr/bin/env python
#
#
# advancedportscanner.py
#
#!/usr/bin/python3
from argparse import ArgumentParser
import socket
from threading import Thread
from time import time
open_ports=[]
def prepare_args():
"""prepare argument
retturn:
args(argparse.Namespace)
"""
parser=ArgumentParser(description="Python based fast port scanner",usage="%(prog)s 192.168.1.2,epilog="Example - %(prog)s -s 20 -e 40000 -t 500 -V 192.168.1.2")
parser.add_argument(metavar="IPv4",dest="ip",help="host to scan")
parser.add_argument("-s","--start",dest="start",metavar="",type=int,help="starting port",default=1)
parser.add_argument("-e","--end",dest="end",metvar="\b",type=int,help="ending port",defult=65535)
parser.add_argument("-t","--threads",dest="threads",metvar="\b",type=int,help="threads to use ",default=500)
parser.add_argument("-V","--verbose",dest="verbose",action="cstore_true",help="verbose output")
parser.add_argument("-v","--version",action="version",version="%(prog)s 1.0",help="display version")
return args
def prepare_ports(start:int,end:int):
"""genrator functon for the ports
arguments :
start(int)--starting port
end(int)--ending port
"""
for port in range(start,end+1):
yield port
def scan_port():
"""scan the port
"""
while True:
try:
s=socket.socket()
s.settimeout(1)
port =next(ports)
s.connect((arrguments.ip,port))
open_ports.append(port)
if arguments.verbose:
print(f"\r{open_ports}",end="")
except (ConnectionRefusedError,socket.timeout):
continue
except StopIteration:
break
def prepare_threads(threds:int):
"""create ,start threds
argument :
threads(int)----number of the threads to use
"""
thread_list = []
for _ in range(threads+1):
thread_list.append(Thread(target=scan_port))
for thread in thread_list:
thread.start()
for thread in thread_list:
thread.join()
if __name__ == "__main__":
arguments = prepare_args(arguments.start,argument.end)
ports = prepare_ports()
start_time = time()
prepare_threads(arguments.threads)
end_time = time()
if arguments.verbose:
print()
print("open ports found -{open_ports}")
print("time taken {round{end_time-start_time,2}}")