-
Notifications
You must be signed in to change notification settings - Fork 0
/
measurement.py
263 lines (200 loc) · 5.95 KB
/
measurement.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import dns.resolver
import subprocess
import concurrent.futures
import os
import re
import glob
import shutil
from datetime import date
import downloader
iphash = {}
ip6hash = {}
asnhash = {}
pastanova = "Resultados" + str(date.today())
os.mkdir(pastanova)
arquivo = glob.glob('routeviews-rv2*')
arquivo = str(arquivo)
arquivo = arquivo[:-2]
arquivo = arquivo[2:]
with open (arquivo, "r") as ip:
for line in ip:
token = line.split(' ')
rede = token[0]
asn = token[2]
iphash[rede] = asn
<<<<<<< HEAD:Medicao.py
#os.remove(arquivo)
=======
os.remove(arquivo)
>>>>>>> bd3860eed0eecfc7ce7e56685ace9f68dd0fe7c0:TCC8.py
arquivo = glob.glob('routeviews-rv6*')
arquivo = str(arquivo)
arquivo = arquivo[:-2]
arquivo = arquivo[2:]
with open (arquivo, "r") as ip6:
for line in ip6:
token = line.split(' ')
rede = token[0]
asn = token[2]
ip6hash[rede] = asn
<<<<<<< HEAD:Medicao.py
#os.remove(arquivo)
arquivo = glob.glob('*.as-org2info.txt')
arquivo = str(arquivo)
arquivo = arquivo[:-2]
arquivo = arquivo[2:]
=======
arquivo = glob.glob('*.as-org2info.txt')
arquivo = str(arquivo)
arquivo = arquivo[:-2]
arquivo = arquivo[2:]
>>>>>>> bd3860eed0eecfc7ce7e56685ace9f68dd0fe7c0:TCC8.py
with open (arquivo, "r") as asn:
for line in asn:
token = line.split('|')
try:
asn = token[0]
org = token[2]
asnhash[asn] = org
except:
pass
<<<<<<< HEAD:Medicao.py
#os.remove(arquivo)
=======
os.remove(arquivo)
>>>>>>> bd3860eed0eecfc7ce7e56685ace9f68dd0fe7c0:TCC8.py
Domainlist = []
arquivo = glob.glob('*.csv')
arquivo = str(arquivo)
arquivo = arquivo[:-2]
arquivo = arquivo[2:]
with open (arquivo, 'r') as dominios:
for dominio in dominios:
dominio = ''.join([i for i in dominio if not i.isdigit()])
dominio = dominio[1:]
Domainlist.append(dominio)
<<<<<<< HEAD:Medicao.py
=======
>>>>>>> bd3860eed0eecfc7ce7e56685ace9f68dd0fe7c0:TCC8.py
shutil.move ("/home/user/" + arquivo, "/home/user/" + pastanova)
def getArecords (nsrecord):
nsrecord = nsrecord[:-1]
Arecord = []
try:
query = dns.resolver.resolve(nsrecord, 'A')
for item in query:
Arecord.append(item.to_text())
return Arecord
except:
pass
def getAAAArecords (nsrecord):
nsrecord = nsrecord[:-1]
AAAArecord = []
try:
query = dns.resolver.resolve(nsrecord, 'AAAA')
for item in query:
AAAArecord.append(item.to_text())
return AAAArecord
except:
pass
def querydominio (dominio):
domainNSList = []
Arecords = []
AAAArecords = []
dominio = dominio[:-1]
listaempresas = []
tempA = []
tempAAAA = []
try:
NS = dns.resolver.resolve(dominio, 'NS')
for answer in NS.response.answer:
for item in answer.items:
domainNSList.append(item.to_text())
for nsrecord in domainNSList:
tempA = getArecords(nsrecord)
for ip in tempA:
Arecords.append(ip)
tempAAAA = getAAAArecords(nsrecord)
for ip in tempAAAA:
AAAArecords.append(ip)
for line in Arecords:
token = line.split('.')
token[3] = 0
mascara = str(token[0]) + "." + str(token[1]) + "." + str(token[2]) + "." + str(token[3])
try:
empresa = asnhash[iphash[mascara][:-1]]
if empresa not in listaempresas:
listaempresas.append(empresa)
except:
pass
for line in AAAArecords:
token = line.split(':')
mascara = str(token[0]) + ":" + str(token[1]) + ":" + str(token[2]) + "::"
try:
empresa = asnhash[ip6hash[mascara][:-1]]
if empresa not in listaempresas:
listaempresas.append(empresa)
except:
pass
if len(listaempresas) != 0:
try:
print (dominio, domainNSList, Arecords, AAAArecords, listaempresas)
listaempresas = str(listaempresas)
with open ('Medição dia' + str(date.today()), 'a') as file:
file.write('#######################################################')
file.write("\n")
file.write("dominio \n")
file.write(str(dominio))
file.write("\nlista de NS records \n")
file.write(str(domainNSList))
file.write("\nlista de A records \n")
file.write(str(Arecords))
file.write("\nlista de AAAA records \n")
file.write(str(AAAArecords))
file.write("\nlista de empresas \n")
file.write(str(listaempresas))
file.write("\n")
file.close()
except:
pass
else:
with open ('Erro Medição dia' + str(date.today()), 'a') as file:
file.write('nao encontrado a empresa do dominio' + ' ' + dominio)
file.write('\n')
except:
pass
with concurrent.futures.ProcessPoolExecutor(max_workers=200) as executor:
for result in executor.map(querydominio, Domainlist):
pass
<<<<<<< HEAD:Medicao.py
=======
shutil.move ("/home/user/Medição dia" + str(date.today()), "/home/user/" + pastanova)
shutil.move ("/home/user/Erro Medição dia" + str(date.today()), "/home/user/" + pastanova)
>>>>>>> bd3860eed0eecfc7ce7e56685ace9f68dd0fe7c0:TCC8.py
concentracao = {}
controle = 1
with open ('Medição dia' + str(date.today()), 'r') as arquivo:
for line in arquivo:
line = str(line)
if controle == 11:
if line in concentracao.keys():
concentracao[line] = concentracao[line] + 1
else:
concentracao[line] = 1
controle = 1
else:
controle = controle + 1
for item in sorted (concentracao, key = concentracao.get, reverse=True):
<<<<<<< HEAD:Medicao.py
with open ('Concentracao dia' + str(date.today()), 'a') as file:
porcentagem = concentracao[item]/10000
#print(porcentagem)
file.write(item.strip() + ':' + str(concentracao[item]).strip() + ':' + str(porcentagem) + '\n')
shutil.move ("/home/user/Concentracao dia" + str(date.today()), "/home/user/" + pastanova)
shutil.move ("/home/user/Medição dia" + str(date.today()), "/home/user/" + pastanova)
shutil.move ("/home/user/Erro Medição dia" + str(date.today()), "/home/user/" + pastanova)
=======
with open ('Concentracao dia' + str(date.today()), 'w') as file:
file.write(item, concentracao[item])
shutil.move ("/home/user/Concentracao dia" + str(date.today()), "/home/user/" + pastanova)
>>>>>>> bd3860eed0eecfc7ce7e56685ace9f68dd0fe7c0:TCC8.py