-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortScan.py
54 lines (41 loc) · 1.27 KB
/
PortScan.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
import sys
import argparse
import socket
import time
def main():
#if sys.version_info[0] != 2 or sys.version_info[1] != 7:
# print("This script requires Python version 2.7")
# sys.exit(1)
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("scanner.txt", "w")
start_time = time.time()
for i in range(65536):
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"
f.write(string_out)
except:
string_out = str(i) + " (NA) was open\n"
f.write(string_out)
except:
num_open -= 1
total_time = time.time() - start_time
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()