-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
52 lines (43 loc) · 1.41 KB
/
utils.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
import json
from socket import gethostbyname, socket, AF_INET, SOCK_STREAM
import time
import sys
import requests
def is_url_available(url):
try:
response = requests.get(url)
if response.status_code == 200:
return True
except Exception as ex:
print(str(ex))
return False
return False
def print_on_console(method_name, target, results, json_output=True):
print("*" * 12, file=sys.stderr)
print("{} for {}".format(method_name, target), file=sys.stderr)
if json_output:
print(json.dumps(results, indent=4), file=sys.stderr)
else:
print(results, file=sys.stderr)
print("=" * 12, file=sys.stderr)
def get_formatted_time(value):
return "{:0.2f}".format(value)
def get_open_ports(target, common=False, start_port=80, end_port=100):
start_time = time.time()
host_ip = gethostbyname(target)
open_ports = []
# Common ports
# 80 (http), 443 (https)
# SSH (port 22), FTP (port 21)
# DNS (port 53), POP3 (port 110)
ports = [80, 443, 22, 21, 53, 110]
if not common:
ports = list([i for i in range(start_port, end_port)])
for i in ports:
connection = socket(AF_INET, SOCK_STREAM)
status = connection.connect_ex((host_ip, i))
if status == 0:
open_ports.append(i)
connection.close()
total_time = time.time() - start_time
return sorted(open_ports), total_time