+ mac_to_ddm = parse_json_dns(sys.argv[1])
+ for mac in mac_to_ddm:
+ ddm = mac_to_ddm[mac]
+ ddm.print_mappings()
+ # maps_tuple = parse_json_dns(sys.argv[1])
+
+ # # print hostname to ip map
+ # hn_ip_map = maps_tuple[0]
+ # for hn in hn_ip_map.keys():
+ # print "====================================================================="
+ # print hn, "maps to:"
+ # for ip in hn_ip_map[hn]:
+ # print " -", ip
+ # print "====================================================================="
+
+ # print " "
+
+ # # print ip to hostname map
+ # ip_hn_map = maps_tuple[1]
+ # for ip in ip_hn_map.keys():
+ # print "====================================================================="
+ # print ip, "maps to:"
+ # for hn in ip_hn_map[ip]:
+ # print " -", hn
+ # print "====================================================================="
+
+class DeviceDNSMap:
+ def __init__(self, mac_address):
+ # MAC address of device
+ self.mac = mac_address
+ # Maps an external IP to a list of (timestamp,hostname) tuples.
+ # Entries in the list should be interpreted as follows:
+ # the timestamp indicates WHEN this device mapped the given ip (key in dict) to the hostname.
+ self.ip_mappings = defaultdict(list)
+
+ def hostname_for_ip_at_time(self, ip, timestamp):
+ # Does device have a mapping for the given IP?
+ if not ip in self.ip_mappings:
+ return None
+ if not self.ip_mappings[ip]:
+ # If list of (timestamp,hostname) tuples is empty, there is no mapping to report.
+ return None
+ # Best fit mapping: the mapping immediately BEFORE timestamp parameter.
+ # Start with random pick (element 0).
+ best_fit = self.ip_mappings[ip][0]
+ for t in self.ip_mappings[ip]:
+ # t is a (timestamp,hostname) tuple
+ if t[0] < timestamp and t[0] > best_fit[0]:
+ # t is a better fit if it happened BEFORE the input timestamp
+ # and is LATER than the current best_fit
+ best_fit = t
+ return best_fit
+
+ def add_mapping(self, ip, timestamp_hostname_tuple):
+ self.ip_mappings[ip].append(timestamp_hostname_tuple)
+
+ def print_mappings(self):
+ count = 0
+ print "### Mappings for MAC = ", self.mac, "###"
+ for ip in self.ip_mappings:
+ print "--- IP ", ip, " maps to: ---"
+ for t in self.ip_mappings[ip]:
+ print t[1], "at epoch time =", t[0]
+ count += 1
+ print "### Total of", count, "mappings for", self.mac, "###"
+
+ # --------------------------------------------------------------------------
+ # Define eq and hash such that instances of the class can be used as keys in dictionaries.
+ # Equality is based on MAC as a MAC uniquely identifies the device.
+ def __eq__(self, another):
+ return hasattr(another, 'mac') and self.mac == another.mac
+ def __hash__(self):
+ return hash(self.data)
+ # --------------------------------------------------------------------------
+
+
+def parse_json_dns(file_path):
+ # Our end output: dictionary of MAC addresses with DeviceDNSMaps as values.
+ # Each DeviceDNSMap contains DNS lookups performed by the device with the corresponding MAC.
+ result = defaultdict()