-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_scanner.py
70 lines (46 loc) · 1.77 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import socket
import re
import common_ports
ports_and_services = common_ports.ports_and_services
def check_valid_hostname(target):
valid_hostname_regex = "^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?<!-)$"
is_valid_hostname = re.match(valid_hostname_regex, target)
return is_valid_hostname
def get_host(ip):
try:
host = socket.gethostbyaddr(ip)[0]
return host
except:
return ip
def get_open_ports(target, port_range, verbose=False):
open_ports = []
try:
ip = socket.gethostbyname(target)
host_info = socket.gethostbyname_ex(target)
host = get_host(ip) if host_info[0] == ip else host_info[0]
for port in range(port_range[0], port_range[1] + 1):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
result = s.connect_ex((ip, port))
if result == 0:
open_ports.append(port)
s.close()
if verbose:
title = 'Open ports for ' + host + \
' (' + ip + ')' if host != ip else 'Open ports for ' + ip
description_list = [title, 'PORT SERVICE']
for port in open_ports:
space = ' ' * max((9 - len(str(port))), 0)
row = str(port) + space + \
ports_and_services[port] if port in ports_and_services else str(
port)
description_list.append(row)
return '\n'.join(row for row in description_list)
except socket.gaierror:
is_valid_hostname = check_valid_hostname(target)
if is_valid_hostname:
return 'Error: Invalid hostname'
return 'Error: Invalid IP address'
except socket.error:
print(socket.error)
return(open_ports)