> self.new_port_dict = {}#字典来存储新端口
来自curr_host
> self.old_port_dict = {}#Dictionary用于存储old_host中的旧端口
> self.results_ports_dict = {}#保存已更改/新添加端口的结果
该脚本需要比较哪个端口发生了变化,我几乎无法提供帮助我:
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
这给出了一个输出:
The following Host/ports were available in old scan : !!
{'172.16.0.41': set([(80,'tcp'),(666,'tcp')]),'172.16.0.163': set([(80,(22,'tcp')])}
--------------------------------------------------------
The following Host/ports have been added in new scan: !!
{'172.16.0.41': set([(80,'tcp')])}
Result Change: for 172.16.0.41 -> set([(22,'tcp')]) From set([(80,'tcp')])
Result Change: for 172.16.0.163 -> set([]) From set([(80,'tcp')])
正如你可以清楚地看到的那样,我也得到了改变后的字典.我想打印:
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')
最佳答案
我相信你不是在比较字典,而是实际上与键对应的值.
原文链接:/python/438478.html这里的基本想法是:
>主机不能总是出现在过去和现在的扫描中,并且会使用collections.defaultdict,以确保即使在没有主机的情况下也可以直接比较值.因为将自动生成缺失键的值(空集)
>在端口集上有3个操作
>& (交叉点):查看扫描中哪些端口保持不变(相同端口)
> old – new:查看哪些端口在旧扫描中但不再在新的(已删除端口)中
> new – old:查看哪些端口在新扫描中但不在旧端口(添加端口)
希望能帮助到你
import collections
scan0 = collections.defaultdict(set,{
'172.16.0.41': set([(80,'tcp')])
})
scan1 = collections.defaultdict(set,'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,port[1])
print()
print('=' * 10,'Deleted')
for host,ports in scan_del.items():
print(host,port[1])
这将输出:
---------- 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