Basically this is a code modification of a previous question i saw on stack-overflow & I found it interesting to pursue which deals with port scanning . In this code I am trying to compare 2 pickle files which hold the scan result of 2 scans performed one after another. I am interested in finding
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)
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] = collections.defaultdict(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] = collections.defaultdict(set())
for x in s.get_open_ports():
self.new_port_dict[s.address].add(x)
hosts = sorted(set(self.prev_report.hosts.keys() + self.report.hosts.keys()))
for host in hosts:
scan_same[host] = self.prev_report.hosts[host] & self.report.hosts[host]
scan_new[host] = self.report.hosts[host] - self.prev_report.hosts[host]
scan_del[host] = self.prev_report.hosts[host] - self.report.hosts[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])
except Exception as l:
print l
But the code throws :
first argument must be callable
Help me to use collection efficiently.
P.S : Trying to improve this way How to compare dictionaries and see what changed?