-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_flows.py
More file actions
277 lines (257 loc) · 7.88 KB
/
Copy pathget_flows.py
File metadata and controls
277 lines (257 loc) · 7.88 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#-*—coding:utf8-*-
'''
This file is used to get iw, queu, beacon,
drop information for the flows generated
by mining_retrans.py.
'''
import numpy as np
import gc
import re
import csv
import codecs
channel_ssid_time = {}
channel_record = {}
calculate = {}
# maclist = ("04:a1:51:96:ca:83",
# "04:a1:51:a3:57:1a",
# "44:94:fc:82:d9:8e",
# "04:a1:51:a8:6e:c5",
# "04:a1:51:96:64:cb",
# "04:a1:51:a0:65:c0",
# "04:a1:51:a7:54:f9",
# "04:a1:51:8e:f1:cb")
flows = []
try:
fil1 = open("/home/lin/working/codes/data/b.txt", "r")
except Exception:
print "data categories open failed."
exit()
def parse_iw(filename, filename_w, mac_addr, eth_src, eth_dst):
"""
@brief This function get iw for specified flow out of raw
iw informations
@param filename raw iw file handle
@param filename_w iw information for specified data flow
@param mac_addr AP address
@param eth_src used to get station address
@param eth_dst used to get station address
@return { description_of_the_return_value }
"""
try:
f_iw = open(filename, "r")
f_w = open(filename_w, "w")
f_write = csv.writer(f_w)
except Exception:
print filename, " open failed"
exit()
result = f_iw.readlines()
# print mac_addr, eth_src, eth_dst, filename, filename_w
# exit()
for res in result:
res = re.split(", ", res)
if len(res) < 20:
print res
continue
# print res
mac_tmp = res[2]
mac_tmp = mac_tmp.replace('"', "")
# print mac_tmp, mac_addr
if mac_tmp != mac_addr:
continue
station = res[1]
station = station.replace('"', "")
# print station, eth_src, eth_dst
if (station != eth_src) and (station != eth_dst):
continue
# print mac_addr, station
# print res
res[0] = np.uint64(res[0])
res[1] = res[1].replace('"', "")
res[2] = res[2].replace('"', "")
for i in (3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16, 17, 18, 19):
res[i] = int(res[i])
tmp = res[14]
tmp = tmp.replace('"', "")
tmp = re.split(" ", tmp)
try:
tmp = float(tmp[0])
except Exception:
pass
res[14] = tmp
# print tmp
# exit()
tmp = res[15]
tmp = tmp.replace('"', "")
tmp = re.split(" ", tmp)
try:
tmp = float(tmp[0])
except Exception:
pass
res[15] = tmp
# print res
# exit()
f_write.writerow(res)
if f_iw:
f_iw.close()
if f_w:
f_w.close()
def parse_queue(filename, c_w, mac_tmp):
"""
@brief get queue information for specified flow
@param filename queue information handl
@param c_w output file handle
@param mac_tmp AP address
@return null
"""
try:
f_queue = open(filename, "r")
f_w = open(c_w, "w")
f_write = csv.writer(f_w)
except Exception:
print filename, " open failed"
exit()
result = f_queue.readlines()
for res in result:
res = re.split(", ", res)
try:
(mac_addr, time, queue_id, bytes, packets,
qlen, backlog, drops, requeues) = res
except Exception:
print res
continue
mac_addr = mac_addr.replace('"', "")
if mac_addr == mac_tmp:
time = np.uint64(time)
# print len(res)
for i in range(2, 9):
try:
res[i] = int(res[i])
except Exception:
pass
f_write.writerow(res)
if f_queue:
f_queue.close()
if f_w:
f_w.close()
def parse_beacon(filename, c_w, mac_tmp):
try:
f_beacon = open(filename, "r")
f_w = open(c_w, "w")
f_write = csv.writer(f_w)
except Exception:
print filename, " open failed"
exit()
result = f_beacon.readlines()
for res in result:
# print res
res = re.split(", ", res)
try:
(time, timestamp, data_rate, current_channel, channel_type,
ssl_signal, bw, bssid, essid, mac_addr) = res
except Exception:
continue
time = np.uint64(time)
mac_addr = mac_addr.replace('\n', "")
mac_addr = mac_addr.replace('"', "")
if mac_addr != mac_tmp:
continue
res[9] = mac_addr
for i in (1, 2, 3, 5, 6):
res[i] = int(res[i])
for i in (4, 7, 8, 9):
res[i] = res[i].replace('"', "")
# print res
f_write.writerow(res)
def parse_dropped(filename, c_w, mac_tmp):
try:
f_dropped = open(filename, "r")
f_w = open(c_w, "w")
f_write = csv.writer(f_w)
except Exception:
print filename, " open failed"
exit()
result = f_dropped.readlines()
for res in result:
res = re.split(", ", res)
try:
(ip_src, ip_dst, mac_addr, port_src, port_dst,
drop_location, sequence, ack_sequence, time) = res
except Exception:
continue
for i in range(0, 3):
res[i] = res[i].replace('"', "")
if res[2] != mac_tmp:
continue
for i in range(3, 9):
res[i] = int(res[i])
# print res
# print mac_addr
f_write.writerow(res)
catalog = fil1.readlines()
for k in catalog:
"""
Open every file begin with 314, 315, etc. and end with
.csv, read the first line to get the flow, and then
get iw, queue, beacon, drop information from raw information
files.
"""
receord_winsize = {}
record_w = {}
find_the_max = 0
find_the_max_index = ""
try:
fil5 = open(k.replace("\n", "") + ".csv", "r")
write_record = csv.reader(fil5)
except Exception:
print "tranningdata open failed"
exit()
for m in write_record:
break
tmp = m[0]
tmp_len = len(tmp)
tmp = tmp[tmp_len - 3:tmp_len]
m[0] = tmp
print m
(name, mac_addr, eth_src, eth_dst, srcport,
dstport, tmz) = m
print name, mac_addr, eth_src, eth_dst, srcport, dstport, tmz
tmp_string = '/' + mac_addr + "_" + "eth_src" + "_" + \
"eth_dst" + "_" + str(srcport) + "_" + str(dstport) + "_" + "iw.csv"
flows.append(m)
if fil5:
fil5.close()
c_i = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + str('/iw.txt')
c_w = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + tmp_string
parse_iw(c_i, c_w, mac_addr, eth_src, eth_dst)
c_i = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + str('/queue.txt')
tmp_string = '/' + mac_addr + "_" + "eth_src" + "_" + \
"eth_dst" + "_" + str(srcport) + "_" + str(dstport) + "_" + "queue.csv"
c_w = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + tmp_string
parse_queue(c_i, c_w, mac_addr)
# exit()
c_i = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + str('/beacon.txt')
tmp_string = '/' + mac_addr + "_" + "eth_src" + "_" + \
"eth_dst" + "_" + str(srcport) + "_" + \
str(dstport) + "_" + "beacon.csv"
c_w = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + tmp_string
parse_beacon(c_i, c_w, mac_addr)
# exit()
c_i = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + str('/dropped.txt')
tmp_string = '/' + mac_addr + "_" + "eth_src" + "_" + \
"eth_dst" + "_" + str(srcport) + "_" + \
str(dstport) + "_" + "dropped.csv"
c_w = "/home/mysql/home/lin/data/data/" + \
k.replace("\n", "") + tmp_string
parse_dropped(c_i, c_w, mac_addr)
print flows
if fil1:
fil1.close()
del flows, catalog
gc.collect()