-
Notifications
You must be signed in to change notification settings - Fork 0
/
port-scanner.py
55 lines (48 loc) · 1.76 KB
/
port-scanner.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
import pyfiglet
import termcolor
import sys
import socket
import datetime
# Starting Banner
ascii_banner = pyfiglet.Figlet(font='slant')
print(termcolor.colored(ascii_banner.renderText('port scanner'), 'green'))
print(" ")
print(" #####################################################")
print(" # #")
print(" # Port scanner tool #")
print(" # #")
print(" #####################################################")
print(" ")
# Defining a target
if len(sys.argv) == 2:
target = socket.gethostbyname(sys.argv[1]) # translate hostname to IPv4
else:
print("Invalid amount of Arguments")
print("Syntax: python port_scanner.py <ip>")
# Add pretty Banner
print("-" * 50)
print("Scanning Target: "+target)
print(f"Scanning started at: {str(datetime.datetime.now())}")
print("-" * 50)
print()
print("-" * 50)
start = int(input("Enter the starting port number: "))
end = int(input("Enter the ending port number: "))
print("-" * 50)
try:
for port in range(start, end): # max range of port is from 0 to 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("\n Exiting Program....")
sys.exit()
except socket.gaierror:
print("\n Hostname Could Not Be Resolved !!!!")
sys.exit()
except socket.error:
print("\n Server not responding !!!!")
sys.exit()