-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_flows.py
More file actions
67 lines (54 loc) · 1.97 KB
/
Copy pathanalyze_flows.py
File metadata and controls
67 lines (54 loc) · 1.97 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
import sys
import capture_packets
import classifier # helper functions to create feature vectors from flow strings
from classifyFlows import load_model
import flow
def main():
"""
Label flows on the fly. Assumes that input is:
- Coming from stdin
- Coming from capture_packets.py (or is similarly formated)
Will loop infinitely, must be stopped by KeyboardInterrupt
"""
# Python treats sys.std* like a File
flow_input = sys.stdin
clf = load_model('./classifer.randomforest')
# Track number of bursts to match desired output
burst_counter = 0
# Just in case there are empty spaces or junk data,
# seek to the line that is the start of a burst
find_start_of_burst()
current_flows = []
flow_features = []
while True:
# We know that our assumed input has no spaces
# between flows when inside a burst, so it is
# safe to assume that readline() will return a flow
raw_flow = flow_input.readline()
if "END OF BURST" not in raw_flow:
current_flows.append(raw_flow)
flow_features.append(classifier.extract_features(classifer.parse_flow(raw_flow)))
else:
# attempt to label the flows
preds = clf.predict(flow_features)
labels = classifier.map_predictions_to_strings(preds)
# handle outputing the flow information
burst_counter += 1
print("Burst {}:".format(burst_counter))
# `zip()` returns an iterable in O(1)
for flow, label in zip(current_flows, lables):
print(flow + " <" +label+">")
# Reset the tracking variables
current_flows = []
flow_features = []
find_start_of_burst()
def find_start_of_burst(f):
"""
Seaches for the ===START OF BURST===
"""
while True:
line = f.readline()
if "START OF BURST" in line:
return line
if __name__ == "__main__":
main()