-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
39 lines (32 loc) · 893 Bytes
/
__init__.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
import re
import subprocess
startupinfo = subprocess.STARTUPINFO()
creationflags = 0 | subprocess.CREATE_NO_WINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
invisibledict = {
"startupinfo": startupinfo,
"creationflags": creationflags,
}
dynp = set(range(49152, 65535))
regp = set(range(1025, 49151))
def get_all_used_ports():
fp = subprocess.run(
[
"netstat",
"-n",
],
**invisibledict,
capture_output=True
)
return set(
[
int(x)
for x in list(set(re.findall(rb"\b\d+\.\d+.\d+.\d+:(\d+)\b", fp.stdout)))
]
)
def get_dynamic_ports(qty=1):
usedports = get_all_used_ports()
return list(dynp - usedports)[:qty]
def get_registered_ports(qty=1):
usedports = get_all_used_ports()
return list(regp - usedports)[:qty]