如何在打印结果时添加标记和区域名称

时间:2016-06-21 10:20:22

标签: python

这基本上是学习字典映射的工作,基本上我有一个函数可以打印端口的变化,代码如下:

 def comp_ports(self,filename,mapping):
      try:
            #print "HEYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
            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]
            ################### The following code is intensive  ###################

            print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
            diff_key=[key for key in self.old_port_dict if self.old_port_dict[key]!=self.new_port_dict[key]]
            for key in diff_key:
                 print "For %s, Port changed from %s to %s" %(key,self.old_port_dict[key],self.new_port_dict[key])

我称之为主要功能的方式是

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "Usage:\n\tportwatch.py <configfile> [clean]"
        sys.exit(-1)
    else:

        # Read
        config = ConfigParser.ConfigParser()
        config.read(sys.argv[1])

        if len(sys.argv) > 2:
            if sys.argv[2] == "clean":
                for f in ['nmap-report-old.pkl','nmap-report.pkl']:
                    try:
                        os.remove( config.get('system','scan_directory') + "/" + f )
                    except Exception as e:
                        print e

        # Configure Scanner
        s = Scanner(config)

        # Execute Scan and Generate latest report
        net_range = gather_public_ip()  #config.get('sources','networks')  # gather_public_ip()
        ### r = s.run(','.join([[i[0] for i in v] for v in net_range][0]))               
        r = s.run(net_range)

        data = list(itertools.chain(*net_range))
        mapping = {i[0]:[i[1],i[2]] for i in data}

        s.save()
        report = Report(r)

        report.dump_raw(mapping)   ## change made for dump to dump_raw
        print "Hosts in scan report",report.total_hosts()
        # Read in last scan
        report.compare(config.get('system','scan_directory') + '/nmap-report-old.pkl' )
        print "New Hosts"
        report.new_hosts()

#        slack.api_token = config.get('notification','slack_key')
        notify_slack_new_host(report.new_hosts())   #Notifty Slack for any new added host
#        for h in report.result_port_dict.keys():
#           notify_slack(report.new_hosts(h))
        print "Lost Hosts"
        report.lost_hosts()

        report.comp_ports(config.get('system','scan_directory') + '/nmap-report-old.pkl',mapping)

整个代码位于http://pastebin.com/iDYBBrEq,有人可以在comp_ports帮助我,我还要将标记和区域名称添加为dump_raw的similer。

请帮忙

1 个答案:

答案 0 :(得分:0)

由于IP是您在词典old_port_dictnew_port_dictmapping以及mapping中的关键,因此每个IP都会映射到索引为0且区域为的区域的列表索引1,访问那些的方式。

for key in diff_key:
     print "For %s with tag %s and region %s, Port changed from %s to %s" %(key,mapping[key][0],mapping[key][1],self.old_port_dict[key],self.new_port_dict[key])