/
format.py
75 lines (62 loc) · 1.89 KB
/
format.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
71
72
73
74
75
import ipcalc
class Format:
def __init__(self):
self.target_list = []
self.result = []
self.port = 443
def ImportTargetFromFile(self, filename):
with open(filename) as file:
self.target_list = file.read().splitlines()
def ImportTargetFromIp(self, ip):
if not self.target_list:
self.target_list.append(ip)
def FormatTarget(self, target):
ret = []
# 192.168.1.1 192.168.1.1:80 192.168.1.1/16
if len(target.split(':')) == 2:
# 192.168.1.1:80
ip = target.split(':')[0]
port = target.split(':')[1]
ret.append([ip, port])
elif len(target.split('/')) == 2:
# 192.168.1.1/16
ip_list = ipcalc.Network(target)
for ip in ip_list:
ret.append([str(ip), self.port])
else:
# 192.168.1.1
ret.append([target, self.port])
return ret
def GetTarget(self):
self.result = []
for target in self.target_list:
self.result.extend(self.FormatTarget(target))
ret = self.result
return ret
def __del__(self):
# print('del class')
self.target_list = []
self.result = []
self.port = 443
if __name__ == "__main__":
Formata = Format()
Formata.port = 33233
Formata.ImportTargetFromIp('192.168.1.1')
a = Formata.GetTarget()
del Formata
Formata = Format()
Formata.port = 33233
Formata.ImportTargetFromIp('192.168.2.1/29')
b = Formata.GetTarget()
del Formata
Formata = Format()
Formata.port = 33233
Formata.ImportTargetFromIp('192.168.3.1:4444')
c = Formata.GetTarget()
del Formata
Formata = Format()
Formata.port = 33233
Formata.ImportTargetFromFile('test.txt')
d = Formata.GetTarget()
del Formata
print('a', a, '\nb', b, '\nc', c, '\nd', d)