-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortScanToo.py
65 lines (48 loc) · 1.69 KB
/
PortScanToo.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
import sys
import argparse
import socket
import time
def main():
args = parse_input()
if args.hostname != None:
connect_to_host(args.hostname)
else:
print("Please provide host to connect to")
exit(0)
def connect_to_host(host):
num_open = 65535
f = open("scannertoo.txt", "w")
start_time = time.time()
new_order_list = [2*i for i in range(32768)]
new_order_list = new_order_list + [(2*i)+1 for i in range(32768)]
strings_to_print = []
order_to_print = []
for i in new_order_list:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, i))
try:
service = socket.getservbyport(i, "tcp")
string_out = str(i) + " (" + service + ") was open\n"
strings_to_print.append(string_out)
order_to_print.append(i)
except:
string_out = str(i) + " (NA) was open\n"
strings_to_print.append(string_out)
order_to_print.append(i)
except:
num_open -= 1
total_time = time.time() - start_time
# idea from geeksforgeeks tutorial: https://www.geeksforgeeks.org/python-returning-index-of-a-sorted-list/
sort_index = [i for i, x in sorted(enumerate(order_to_print), key=lambda x: x[1])]
for i in sort_index:
f.write(strings_to_print[i])
f.write(str(total_time) + "s\n")
f.write(str(total_time / 65535) + "s\n")
f.close()
def parse_input():
parser = argparse.ArgumentParser(description="Port Scanner")
parser.add_argument("hostname")
return parser.parse_args()
if __name__ == "__main__":
main()