def _parse_xml_report(cls, root=None): """ This method parses out a full nmap scan report from its XML root node: <nmaprun>. :param root: Element from xml.ElementTree (top of XML the document) :type root: Element :return: NmapReport object """ nmap_scan = { '_nmaprun': {}, '_scaninfo': {}, '_hosts': [], '_runstats': {} } if root is None: raise NmapParserException("No root node provided to parse XML " "report") nmap_scan['_nmaprun'] = cls.__format_attributes(root) for el in root: if el.tag == 'scaninfo': nmap_scan['_scaninfo'] = cls.__parse_scaninfo(el) elif el.tag == 'host': nmap_scan['_hosts'].append(cls._parse_xml_host(el)) elif el.tag == 'runstats': nmap_scan['_runstats'] = cls.__parse_runstats(el) # else: # print "struct pparse unknown attr: {0} value: {1}".format( # el.tag, # el.get(el.tag)) return NmapReport(nmap_scan)
def parse_fromdict(cls, rdict): """ Strange method which transforms a python dict \ representation of a NmapReport and turns it into an \ NmapReport object. \ Needs to be reviewed and possibly removed. :param rdict: python dict representation of an NmapReport :type rdict: dict :return: NmapReport """ nreport = {} if list(rdict.keys())[0] == "__NmapReport__": r = rdict["__NmapReport__"] nreport["_runstats"] = r["_runstats"] nreport["_scaninfo"] = r["_scaninfo"] nreport["_nmaprun"] = r["_nmaprun"] hlist = [] for h in r["_hosts"]: slist = [] for s in h["__NmapHost__"]["_services"]: cname = "__NmapService__" slist.append( NmapService( portid=s[cname]["_portid"], protocol=s[cname]["_protocol"], state=s[cname]["_state"], owner=s[cname]["_owner"], service=s[cname]["_service"], ) ) nh = NmapHost( starttime=h["__NmapHost__"]["_starttime"], endtime=h["__NmapHost__"]["_endtime"], address=h["__NmapHost__"]["_address"], status=h["__NmapHost__"]["_status"], hostnames=h["__NmapHost__"]["_hostnames"], extras=h["__NmapHost__"]["_extras"], services=slist, ) hlist.append(nh) nreport["_hosts"] = hlist nmapobj = NmapReport(nreport) return nmapobj
def parse_fromdict(cls, rdict): """ Strange method which transforms a python dict \ representation of a NmapReport and turns it into an \ NmapReport object. \ Needs to be reviewed and possibly removed. :param rdict: python dict representation of an NmapReport :type rdict: dict :return: NmapReport """ nreport = {} if list(rdict.keys())[0] == '__NmapReport__': r = rdict['__NmapReport__'] nreport['_runstats'] = r['_runstats'] nreport['_scaninfo'] = r['_scaninfo'] nreport['_nmaprun'] = r['_nmaprun'] hlist = [] for h in r['_hosts']: slist = [] for s in h['__NmapHost__']['_services']: cname = '__NmapService__' slist.append(NmapService(portid=s[cname]['_portid'], protocol=s[cname]['_protocol'], state=s[cname]['_state'], owner=s[cname]['_owner'], service=s[cname]['_service'], service_extras=s[cname]['_service_extras'])) nh = NmapHost(starttime=h['__NmapHost__']['_starttime'], endtime=h['__NmapHost__']['_endtime'], address=h['__NmapHost__']['_address'], status=h['__NmapHost__']['_status'], hostnames=h['__NmapHost__']['_hostnames'], extras=h['__NmapHost__']['_extras'], services=slist) hlist.append(nh) nreport['_hosts'] = hlist nmapobj = NmapReport(nreport) return nmapobj