减去 2 组时无法打印其他详细信息(tag_name 和区域)

Unable to print additional detail (tag_name & region) while subtracting 2 sets()

我有 2 个 pickel 文件,其中包含 IP 地址以及端口,aws_tags 以及与它们相关的区域信息。这基本上是一个端口扫描器,它有一种在找到新 IP 地址时打印的方法。这是通过用 OLD_pickel_scan 减去 NEW_pickel_scan 来完成的,如下所示:

self.prev_hosts = set()
self.curr_hosts = set()

def new_hosts(self)  
    result_new_hosts =  self.curr_hosts - self.prev_hosts

这工作正常并打印在 pickel 报告中添加的新 IP。

现在我还需要添加该 IP 地址的关联标签和区域。我已经在映射中获得了所需的数据:

 mapping = {i[0]:[i[1],i[2]] for i in data}

i[0] 是 IP,i[1] 是标签,i[2] 是地区

所以我正在尝试使用此映射打印标签。

例如,我有另一种方法,当发现非法端口时打印

def dump_raw(self,mapping):
    nmap_report =  self.report
    for host in nmap_report.hosts:
            #print
            if len(host.hostnames):
                tmp_host = host.hostnames.pop()
            else:
                tmp_host = host.address
            print("Nmap scan report for {0} ({1})".format(tmp_host,host.address))
            print("Host is {0}.".format(host.status))

            #val = config.get('ports', 'scan_range')
            #val_known = config.get('ports','known')

            #safe_port = range(*map(int, val.split(',')))
            #known_ports = map(int, val_known.split(','))

            print("  PORT     STATE         SERVICE")

            for serv in host.services:
                if serv.state == "open":
                 ## print ('Illegal Port open :'+str(serv.port) +'/'+str(serv.protocol)+' '+str(serv.service)+', on host=> '+str(host))
                  print ('Illegal Port open :'+str(serv.port) +'/'+str(serv.protocol)+' '+str(serv.service)+', on host=> '+str(host) + ' Tag =' + (mapping[host.address.strip()][0]) + ' Region =' + str(mapping[host.address.strip()][1]))

这就是我使用映射的方式,有人可以帮助我 new_hosts() 吗?

我试过了:

def new_hosts(self,mapping):
        """Return a list of new hosts added in latest scan"""
        result_new_hosts =  self.curr_hosts - self.prev_hosts
        print mapping[result_new_hosts]

它说:TypeError: unhashable type: 'set'

如果我做类似的事情:

def new_hosts(self,mapping):
        """Return a list of new hosts added in latest scan"""
        result_new_hosts =  self.curr_hosts - self.prev_hosts
        print mapping[result_new_hosts]
        nmap_report = self.report
        for host in nmap_report.hosts:
            for serv in host.services:
                print result_new_hosts,mapping[result_new_hosts.address.strip()[0]],mapping[result_new_hosts.address.strip()[1]]
            return (result_new_hosts,mapping[result_new_hosts.address.strip()[0]],mapping[result_new_hosts.address.strip()[1]])

这会打印:

AttributeError: 'set' object has no attribute 'address'
 result_new_hosts =  self.curr_hosts - self.prev_hosts
 print mapping[result_new_hosts]

result_new_hosts 是一个 set,正如错误所说,集合是不可散列的,因此不能存储或在字典中查找。

相反,您应该搜索集合中的每个单独元素:

result_new_hosts =  self.curr_hosts - self.prev_hosts
for result in result_new_hosts:    
    print mapping[result]

UPDATE 如果您想 return 包含 (ip, (tag, region)):

的元组列表
def new_hosts(self, mapping):
    result_new_hosts =  self.curr_hosts - self.prev_hosts
    return [(result, mapping[result]) for result in result_new_hosts]