0

我写了一个端口扫描器,它基本上将新的扫描结果与以前的扫描结果进行比较,然后找到哪些端口已更改/got_add/got_deleted。

比较端口变化的方法如下:

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

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

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

                hosts = sorted(set(self.old_port_dict) | set(self.new_port_dict))

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


prev_set = set(self.prev_report.hosts)
            new_set = set(self.report.hosts)

            scan_same = prev_set & new_set
            scan_new = new_set - prev_set
            scan_del = prev_set - new_set

            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
             raise

根据答案,但这会引发新的异常:

'set' object has no attribute 'items'
Traceback (most recent call last):
  File "portwatch.py", line 316, in <module>
    report.comp_ports(config.get('system','scan_directory') + '/nmap-report-old.pkl')
  File "portwatch.py", line 159, in comp_ports
    for host, ports in scan_same.items():
AttributeError: 'set' object has no attribute 'items'

我如何迭代?

4

3 回答 3

0

问题似乎是您正在使用host(a str) 作为您的索引self.report.hostswhich is a list。您只能在对象上使用str索引,dict但不能在list对象上使用。

于 2016-01-25T05:54:41.407 回答
0

考虑您发布的以下代码(这是引发异常的地方):

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]

您似乎希望获得仅在两个报告中的每一个中找到的公共主机集和主机集。您的代码几乎是正确的,您只是不必要地使用了迭代和索引,其中 set 操作可以立即获得您想要的东西:

prev_set = set(self.prev_report.hosts)
new_set = set(self.report.hosts)

scan_same = prev_set & new_set
scan_new = new_set - prev_set
scan_del = prev_set - new_set
于 2016-01-25T06:12:27.287 回答
0

您一直在强制将端口字典放入集合中。如果您遍历字典,它将仅返回键,因此您输入一组键,很可能是字符串。

.values()首先使用或.items()在 dicts 上时,您可能会更开心。

于 2016-01-25T06:43:27.880 回答