forked from danielwood95/heavyHitters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.py
More file actions
69 lines (58 loc) · 1.51 KB
/
count.py
File metadata and controls
69 lines (58 loc) · 1.51 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
from collections import Counter
import sys
import matplotlib.pyplot as plt
# Run: python count.py src.txt dst.txt
# Where src.txt and dst.txt are simple text files with a list of source and destination IP addresses
# frequency of sources
with open(sys.argv[1], "r") as f:
# count frequency of each address
freqSrc={}
for line in f:
l = line[:-1]
if l in freqSrc:
freqSrc[l] += 1
else:
freqSrc[l] = 1
# create cumulative graph
packetsSrc=[]
sum = 0
f.close()
i=0
print "Top 20 sources (by frequency)"
for key, value in sorted(freqSrc.iteritems(), key=lambda (k,v): (v,k), reverse=True):
if i < 20:
print value, "\t", key
i += 1
packetsSrc.append(value + sum)
sum = value + sum
# print key, value
# frequency of destinations
with open(sys.argv[2], "r") as f:
# count frequency of each address
freqDst={}
for line in f:
l = line[:-1]
if l in freqDst:
freqDst[l] += 1
else:
freqDst[l] = 1
# create cumulative graph
packetsDst=[]
sum = 0
f.close()
i=0
print
print "Top 20 destinations (by frequency)"
for key, value in sorted(freqDst.iteritems(), key=lambda (k,v): (v,k), reverse=True):
packetsDst.append(value + sum)
if i < 20:
print value, "\t", key
i += 1
sum = value + sum
# print key, value
plt.plot(packetsSrc, label='Source Addresses')
plt.plot(packetsDst, label='Destination Addresses')
plt.ylabel("cumulative packets")
plt.xlabel("hitters")
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)
plt.show()