def iterate_elements(osm_file): context = ET.iterparse(osm_file, events) _, root = next(context) for event, elem in context: if event == 'end' and elem.tag in tags: x = c.data(elem) yield x
def aci_response_xml(rawoutput): ''' Handle APIC XML response output ''' # NOTE: The XML-to-JSON conversion is using the "Cobra" convention try: xml = lxml.etree.fromstring(to_bytes(rawoutput)) xmldata = cobra.data(xml) except: e = get_exception() # Expose RAW output for troubleshooting return dict( raw=rawoutput, error_code=-1, error_text="Unable to parse output as XML, see 'raw' output. %s" % e) # Reformat as ACI does for JSON API output try: result = dict(imdata=xmldata['imdata']['children']) except KeyError: result = dict(imdata=dict()) result['totalCount'] = xmldata['imdata']['attributes']['totalCount'] # Handle possible APIC error information return aci_response_error(result)
def response_xml(self, rawoutput): ''' Handle APIC XML response output ''' # NOTE: The XML-to-JSON conversion is using the "Cobra" convention try: xml = lxml.etree.fromstring(to_bytes(rawoutput)) xmldata = cobra.data(xml) except Exception as e: # Expose RAW output for troubleshooting self.error = dict( code=-1, text="Unable to parse output as XML, see 'raw' output. %s" % e) # self.error = dict(code=str(self.status), text="Request failed: %s (see 'raw' output)" % self.response) self.result['raw'] = rawoutput return # Reformat as ACI does for JSON API output try: self.imdata = xmldata['imdata']['children'] except KeyError: self.imdata = dict() self.totalCount = int(xmldata['imdata']['attributes']['totalCount']) # Handle possible APIC error information self.response_error()
def xml_to_json(aci, response_data): ''' This function is used to convert preview XML data into JSON. ''' if XML_TO_JSON: xml = lxml.etree.fromstring(to_bytes(response_data)) xmldata = cobra.data(xml) aci.result['preview'] = xmldata else: aci.result['preview'] = response_data
def xml_to_json(aci, response_data): ''' This function is used to convert preview XML data into JSON. ''' if XML_TO_JSON: xml = lxml.etree.fromstring(to_bytes(response_data)) xmldata = cobra.data(xml) aci.result['diff'] = xmldata else: aci.result['diff'] = response_data
def run_nmscan(pid: int): """ When run, queries the process information by the id provided from the database. Runs the test and returns the ouput of the test to the database :param pid: Id of process to run :return: None """ process = Process.query.filter_by(id=pid).first() scan = Scan.query.filter_by(id=process.scan_id).first() target_obj = Target.query.filter_by(id=scan.target_id).first() target = target_obj.domain nm = NmapProcess(targets=target, options="-sV -Pn -f --mtu 64 -p '*' -O") rc = nm.run_background() process.status = nm.state process.progress = nm.progress process.date_started = datetime.now().isoformat() scan.date_started = datetime.now().isoformat() db.session.commit() if nm.has_failed(): process.output = "nmap scan failed: {0}".format(nm.stderr) db.session.commit() return 1 while nm.is_running(): print("Nmap Scan running: ETC: {0} DONE: {1}%".format(nm.etc, nm.progress)) if int(scan.progress) < int(float(nm.progress)): process.progress = int(float(nm.progress)) scan.progress = int(float(nm.progress)) db.session.commit() sleep(5) process.date_completed = datetime.now().isoformat() scan.date_completed = datetime.now().isoformat() if nm.has_failed(): process.status = nm.state scan.status = nm.state process.output = str(nm.stderr) elif nm.is_successful(): process.status = 3 scan.status = 3 scan.progress = 100 nmap_full_output = json.dumps(cb.data(fromstring(str(nm.stdout)))) nmap_output = Nmap.parse_nmap_output(nmap_full_output) if nmap_output: process.output = json.dumps(nmap_output) scan.output = json.dumps(nmap_output) else: scan.output = None db.session.commit()
def aci_response(rawoutput, rest_type='xml'): ''' Handle APIC response output ''' result = dict() if rest_type == 'json': # Use APIC response as module output try: result = json.loads(rawoutput) except: e = get_exception() # Expose RAW output for troubleshooting result['error_code'] = -1 result[ 'error_text'] = "Unable to parse output as JSON, see 'raw' output. %s" % e result['raw'] = rawoutput return result else: # NOTE: The XML-to-JSON conversion is using the "Cobra" convention xmldata = None try: xml = lxml.etree.fromstring(to_bytes(rawoutput)) xmldata = cobra.data(xml) except: e = get_exception() # Expose RAW output for troubleshooting result['error_code'] = -1 result[ 'error_text'] = "Unable to parse output as XML, see 'raw' output. %s" % e result['raw'] = rawoutput return result # Reformat as ACI does for JSON API output if xmldata and 'imdata' in xmldata: if 'children' in xmldata['imdata']: result['imdata'] = xmldata['imdata']['children'] else: result['imdata'] = dict() result['totalCount'] = xmldata['imdata']['attributes'][ 'totalCount'] # Handle possible APIC error information try: result['error_code'] = result['imdata'][0]['error']['attributes'][ 'code'] result['error_text'] = result['imdata'][0]['error']['attributes'][ 'text'] except KeyError: result['error_code'] = 0 result['error_text'] = 'Success' return result
def imc_response(module, rawoutput, rawinput=''): ''' Handle IMC returned data ''' xmloutput = lxml.etree.fromstring(rawoutput) result = cobra.data(xmloutput) # Handle errors if xmloutput.get('errorCode') and xmloutput.get('errorDescr'): if rawinput: result['input'] = rawinput result['output'] = rawoutput result['error_code'] = xmloutput.get('errorCode') result['error_text'] = xmloutput.get('errorDescr') module.fail_json(msg='Request failed: %(error_text)s' % result, **result) return result
def imc_response(module, rawoutput, rawinput=''): ''' Handle IMC returned data ''' xmloutput = lxml.etree.fromstring(rawoutput) result = cobra.data(xmloutput) # Handle errors if xmloutput.get('errorCode') and xmloutput.get('errorDescr'): if rawinput: result['input'] = rawinput result['output'] = rawoutput result['error_code'] = xmloutput.get('errorCode') result['error_text'] = xmloutput.get('errorDescr') module.fail_json(msg='Request failed: %(error_text)s' % result, **result) return result
def aci_response_xml(result, rawoutput): ''' Handle APIC XML response output ''' # NOTE: The XML-to-JSON conversion is using the "Cobra" convention try: xml = lxml.etree.fromstring(to_bytes(rawoutput)) xmldata = cobra.data(xml) except Exception as e: # Expose RAW output for troubleshooting result.update(raw=rawoutput, error_code=-1, error_text="Unable to parse output as XML, see 'raw' output. %s" % e) return # Reformat as ACI does for JSON API output try: result.update(imdata=xmldata['imdata']['children']) except KeyError: result['imdata'] = dict() result['totalCount'] = xmldata['imdata']['attributes']['totalCount'] # Handle possible APIC error information aci_response_error(result)
def response_xml(self, rawoutput): ''' Handle APIC XML response output ''' # NOTE: The XML-to-JSON conversion is using the "Cobra" convention try: xml = lxml.etree.fromstring(to_bytes(rawoutput)) xmldata = cobra.data(xml) except Exception as e: # Expose RAW output for troubleshooting self.error = dict(code=-1, text="Unable to parse output as XML, see 'raw' output. %s" % e) self.result['raw'] = rawoutput return # Reformat as ACI does for JSON API output try: self.imdata = xmldata['imdata']['children'] except KeyError: self.imdata = dict() self.totalCount = int(xmldata['imdata']['attributes']['totalCount']) # Handle possible APIC error information self.response_error()
This function is used to generate a preview between two snapshots and add the parsed results to the jctanner.network_aci.aci module return data. ''' uri = jctanner.network_aci.aci.url + jctanner.network_aci.aci.filter_string resp, info = fetch_url(jctanner.network_aci.aci.module, uri, headers=jctanner.network_aci.aci.headers, method='GET', timeout=jctanner.network_aci.aci.module.params['timeout'], use_proxy=jctanner.network_aci.aci.module.params['use_proxy']) jctanner.network_aci.aci.method = 'GET' jctanner.network_aci.aci.response = info['msg'] jctanner.network_aci.aci.status = info['status'] # Handle APIC response if info['status'] == 200: xml_to_json(jctanner.network_aci.aci, resp.read()) else: jctanner.network_aci.aci.result['raw'] = resp.read() jctanner.network_aci.aci.fail_json(msg="Request failed: %(code)s %(text)s (see 'raw' output)" % jctanner.network_aci.aci.error) def xml_to_json(jctanner.network_aci.aci, response_data): ''' This function is used to convert preview XML data into JSON. ''' if XML_TO_JSON: xml = lxml.etree.fromstring(to_bytes(response_data)) xmldata = cobra.data(xml) jctanner.network_aci.aci.result['preview'] = xmldata else: jctanner.network_aci.aci.result['preview'] = response_data if __name__ == "__main__": main()