-
Notifications
You must be signed in to change notification settings - Fork 0
/
portscanner2.py
32 lines (31 loc) · 999 Bytes
/
portscanner2.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
#!/bin/python3
import sys, socket
from datetime import datetime
from termcolor import colored
#Accept user input
ipAdrress = input("Enter the Ip address to scan: ")
print("Target IP: "+ipAdrress)
startingPort = input("Enter first port: ")
print("Starting port: "+ startingPort)
endPort = input("Enter last port: ")
print("Last port: "+endPort)
#define target
target = socket.gethostbyname(ipAdrress)
try:
for port in range(int(startingPort), int(endPort)):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = s.connect_ex((target, port))
print("Checking port {}".format(port))
if result == 0:
print (colored("Port {} is open.".format(port),'green'))
s.close()
except KeyboardInterrupt:
print("\n Terminating scan......")
sys.exit()
except socket.gaierror:
print("Host could not be resolved")
sys.exit()
except socket.error:
print("Could not connect to server")
sys.exit()