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
|
import json import re
f = open("network.txt","r") data = json.load(f) dic = {}
for line in data: DestHost = line['DestHost'] rel = re.match("[a-zA-Z]+",DestHost) if rel: ls = DestHost.split(".") length = len(ls) if length == 2: value = dic.get(DestHost,0) dic[DestHost] = value + 1
elif length == 3: value = dic.get(DestHost, 0) dic[DestHost] = value + 1 Host2 = ls[1]+"."+ls[2] value2 = dic.get(Host2,0) dic[Host2] = value2 + 1
elif length == 4: value = dic.get(DestHost, 0) dic[DestHost] = value + 1 Host2 = ls[0] + "." + ls[1] + "." + ls[2] value2 = dic.get(Host2, 0) dic[Host2] = value2 + 1 Host3 = ls[1]+"."+ls[2] value3 = dic.get(Host3, 0) dic[Host3] = value3 + 1
print(sorted(dic.items(), key=lambda item:item[1], reverse=True))
|