-
Notifications
You must be signed in to change notification settings - Fork 9
/
is_port_open.py
70 lines (67 loc) · 2.07 KB
/
is_port_open.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
#! /usr/bin/python3.6
# coding: utf-8
import socket
import random
import threading
import colorama
from termcolor import colored
statistics = []
count_opened = 0
count_closed = 0
count_no_rst = 0
count_dest_unreach = 0
def is_open(ip, end_range=4):
global count_opened
global count_closed
global count_no_rst
global count_dest_unreach
# Генерируем список из рандомных портов
ports = [random.randint(81, 5055) for x in range(end_range)]
# ставим таймаут неответа на первый syn
socket.setdefaulttimeout(2)
opened_ports = []
closed_ports = []
no_rst = []
dest_unreach = []
for port in ports:
try:
conn = socket.socket()
conn.connect((ip, port))
conn.send(b'hello, world!')
data = conn.recv(1024)
conn.close()
opened_ports.append(port)
count_opened+=1
except ConnectionRefusedError:
conn.close()
closed_ports.append(port)
count_closed+=1
except socket.timeout:
conn.close()
no_rst.append (port)
count_no_rst+=1
except OSError:
dest_unreach.append(port)
count_dest_unreach+=1
if not len(opened_ports):
opened_ports.append(0)
if not closed_ports:
closed_ports.append(0)
if not len(no_rst):
no_rst.append(0)
statistics.append('{:*>60}'.format(''))
statistics.append(f'Summary of {ip} check:\n')
statistics.append(f'[f] Answered SYN {opened_ports}\n')
statistics.append(f'[f] Dst unreachable {dest_unreach}\n')
statistics.append(f'[f] Opened ports NO RST {no_rst}\n')
statistics.append(f'[ok] Closed ports RST recieved {closed_ports}\n')
statistics.append('{:*>60}'.format(''))
def conn_threads(function, ip, end_range=4):
threads = []
th = threading.Thread(target = function, args = (ip, end_range))
th.start()
threads.append(th)
return threads
def close_threads(thread):
for th in thread:
th.join()