Nishant Singh
Nishant Singh

Reputation: 3209

How to compare dictionaries and see what changed?

I am having 3 dictionaries in my python code :

  1. self.new_port_dict = {} # Dictionary to store the new ports from curr_host
  2. self.old_port_dict = {} # Dictionary to store the old ports from old_host
  3. self.results_ports_dict = {} # Holds the result of changed/newly added ports

The script needs to compare what port changed, I am almost there just unable to present help me out :

      def comp_ports(self,filename):
          try:
                f = open(filename)
                self.prev_report = pickle.load(f) # NmapReport

                for s in self.prev_report.hosts:
                    self.old_port_dict[s.address] = set()
                    for x in s.get_open_ports():
                        self.old_port_dict[s.address].add(x)

                for s in self.report.hosts:
                    self.new_port_dict[s.address] = set()
                    for x in s.get_open_ports():
                       self.new_port_dict[s.address].add(x)

                print "The following Host/ports were available in old scan : !!"
                print `self.old_port_dict`
                print "--------------------------------------------------------"
                print "The following Host/ports have been added in new scan:  !!"
                print `self.new_port_dict`
                for h in self.old_port_dict.keys():
                 self.results_ports_dict[h] = self.new_port_dict[h]- self.old_port_dict[h]
                 print "Result Change: for",h ,"->",self.results_ports_dict[h]         

 except Exception as l:
             print l

This gives a output as :

The following Host/ports were available in old scan : !!
{'172.16.0.41': set([(80, 'tcp'), (666, 'tcp')]), '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])}
--------------------------------------------------------
The following Host/ports have been added in new scan:  !!
{'172.16.0.41': set([(80, 'tcp'), (22, 'tcp')]), '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])}

Result Change: for 172.16.0.41 -> set([(22, 'tcp')])  From set([(80, 'tcp'), (666, 'tcp')])

Result Change: for 172.16.0.163 -> set([])  From set([(80, 'tcp'), (22, 'tcp')])

As you can clearly see , I have the resulting changed dictionary as well. I want to just print :

    For "host_name" , Port changed from "port_id" to "new_port_id" 

ex: For 172.16.0.41, Port changed from (666, 'tcp')  to  (22, 'tcp')

Upvotes: 5

Views: 273

Answers (3)

mementum
mementum

Reputation: 3203

I believe your are not comparing the dictionaries but actually the values corresponding to the keys.

The basic ideas here are:

  • A host must not always be present in past and present scans and hance the usage of collections.defaultdict, to ensure a straightforward comparison of values even in the absence of a host. Because a value for the missing key will be automatically generated (an empty set)

  • Have 3 operations on the set of ports

    • & (intersection): to see which ports have remained constant across scans (same ports)

    • old - new: to see which ports were in the old scan but no longer in the new (deleted ports)

    • new - old: to see which ports are in the new scan but were not in the old (added ports)

There are several ways to move the code around to optimize, but I guess the point is much more clarity.

Hope it helps

import collections

scan0 = collections.defaultdict(set, {
    '172.16.0.41': set([(80, 'tcp'), (666, 'tcp')]),
    '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])
})

scan1 = collections.defaultdict(set, {
    '172.16.0.41': set([(80, 'tcp'), (22, 'tcp')]),
    '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])
})

hosts = sorted(set(scan0.keys() + scan1.keys()))


scan_same = dict()
scan_new = dict()
scan_del = dict()

for host in hosts:
    scan_same[host] = scan0[host] & scan1[host]
    scan_new[host] = scan1[host] - scan0[host]
    scan_del[host] = scan0[host] - scan1[host]

print()
print('-' * 10, 'Same')
for host, ports in scan_same.items():
    print(host, ':')
    for port in ports:
        print(':::', port[0], '/', port[1])
print()
print('*' * 10, 'Added')
for host, ports in scan_new.items():
    print(host, ':')
    for port in ports:
        print(':::', port[0], '/', port[1])

print()
print('=' * 10, 'Deleted')
for host, ports in scan_del.items():
    print(host, ':')
    for port in ports:
        print(':::', port[0], '/', port[1])

This will output:

---------- Same
172.16.0.163 :
::: 80 / tcp
::: 22 / tcp
172.16.0.41 :
::: 80 / tcp

*********** Added
172.16.0.163 :
172.16.0.41 :
::: 22 / tcp

========== Deleted
172.16.0.163 :
172.16.0.41 :
::: 666 / tcp

Upvotes: 1

Ali
Ali

Reputation: 1635

You need to keep another set like old_port_dict_changed = self.old_port_dict[h] - self.new_port_dict[h] now the changed ports from the old_port_dict are in old_port_dict_changed and the new ports which replaced the old ones are in results_ports_dict. Does that help?

Upvotes: 0

CoMartel
CoMartel

Reputation: 3591

Based on Alex Martelli's answer on Is there a better way to compare dictionary values

You can do this :

#eval the differences between the 2 dict:
diff_key=[key for key in old_port_dict if old_port_dict[key]!=new_port_dict[key]]
for key in diff_key:
    print "For %s, Port changed from %s to %s" %(key,old_port_dict[key],new_port_dict[key])

Upvotes: 2

Related Questions