-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathportscanner.py
49 lines (39 loc) · 1.08 KB
/
portscanner.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
#!/bin/python
import sys
import socket
from datetime import datetime
#Define our targets
if len(sys.argv) ==2:
target = socket.gethostbyname(sys.argv[1]) #Translate hostname to ipv4
else:
print("Invalid amount of arguments.")
print("Syntax: python3 portscanner.py <ip>")
#Add a pretty banner
print("-" * 50)
print("Scanning target "+target)
print("Time started: "+str(datetime.now()))
print("-" * 50)
#check time scan initiated
t1 = datetime.now()
try:
for port in range(50,85): # or scan the whole range 1,65535
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target,port)) #returns an error indicator
if result == 0:
print("Port {}: is open".format(port))
s.close()
except KeyboardInterrupt:
print("\nExiting program.")
sys.exit()
except socket.gaierror:
print("Hostname could not be resolved")
sys.exit()
except socket.error:
print("Couldn't connect to server.")
sys.exit()
#Check time again
t2 = datetime.now()
#Find out How long it took to run the script
total = t2 - t1
print('Scan Completed in:',total)