-
Notifications
You must be signed in to change notification settings - Fork 8
/
fortune_port.py
executable file
·62 lines (43 loc) · 1.53 KB
/
fortune_port.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
#!/usr/bin/env -S python -u
from pathlib import Path
from fire import Fire
from lib.scan import check_port, generate_ips, process_each
__author__ = 'Mikhail Yudin aka fagci'
counter = 0
def check(ip, pl, out, max_count, *scanopts):
global counter
with pl:
if counter >= max_count:
raise StopIteration()
res = check_port(ip, *scanopts)
if res:
_, time = res
with pl:
if counter < max_count: # precision ensurance
counter += 1
print(f'{counter:<4} {ip:<15} {int(time*1000):>4} ms')
out.write(f'{ip}\n')
def check_ips(p: int, c: int = 1024, l: int = 2_000_000, w: int = 1500, t=1.5, f=False, F=False, d=False, i=None):
"""Scan random ips for port
:param int p: port to check
:param int c: needed ips count
:param int l: maximum processing IPs
:param int w: workers count
:param float t: connect timeout
:param bool f: overwrite result file w/prompt
:param bool F: force overwrite result file
:param bool d: double check each host
:param bool i: network interface to use"""
results_file = Path(f'./local/hosts_{p}.txt')
if F:
f = True
elif f:
f = input(f'Delete hosts_{p}.txt? y/n: ').lower() == 'y'
ips = generate_ips(l)
try:
with open(results_file, 'w' if f else 'a') as out:
process_each(check, ips, w, out, c, p, t, d, i)
except KeyboardInterrupt:
print('Interrupted by user.')
if __name__ == "__main__":
Fire(check_ips)