xml_exporter.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-15 -*-
  3. """
  4. BC (Border-Check) is a tool to retrieve info of traceroute tests over website navigation routes.
  5. GPLv3 - 2013-2014-2015 by psy (epsylon@riseup.net)
  6. """
  7. class xml_reporting(object):
  8. """
  9. Print results from a traceroute in an XML fashion
  10. """
  11. def __init__(self, bc):
  12. # initialize main BC
  13. self.instance = bc
  14. def print_xml_results(self, filename):
  15. import xml.etree.ElementTree as ET
  16. root = ET.Element("travel")
  17. i = 1
  18. for i in self.instance.result_list:
  19. hop = ET.SubElement(root, "hop")
  20. host = ET.SubElement(hop, "host")
  21. hop_ip = ET.SubElement(hop, "hop_ip")
  22. longitude = ET.SubElement(hop, "longitude")
  23. latitude = ET.SubElement(hop, "latitude")
  24. city = ET.SubElement(hop, "city")
  25. country = ET.SubElement(hop, "country")
  26. server_name = ET.SubElement(hop, "server_name")
  27. asn = ET.SubElement(hop, "asn")
  28. timestamp = ET.SubElement(hop, "timestamp")
  29. country_code = ET.SubElement(hop,"country_code")
  30. meta = ET.SubElement(hop, "meta")
  31. root.text = i['url']
  32. hop.text = str(i['hop_count'])
  33. host.text = i['destination_ip']
  34. hop_ip.text = i['hop_ip']
  35. longitude.text = i['longitude']
  36. latitude.text = i['latitude']
  37. city.text = i['city']
  38. country.text = i['country']
  39. server_name.text = i['server_name']
  40. asn.text = i['asn']
  41. timestamp.text = i['timestamp']
  42. country_code.text = i['country_code']
  43. meta.text = "Connect here XML metadata"
  44. tree = ET.ElementTree(root)
  45. tree.write(filename)
  46. def read_xml_results(self):
  47. from xml.dom.minidom import parseString
  48. file = open(self.instance.options.import_xml,'r')
  49. data = file.read()
  50. file.close()
  51. dom = parseString(data)
  52. travel_tag = dom.getElementsByTagName('travel')[0].toxml()
  53. travel_data = travel_tag.replace('<travel>','').replace('</travel>','').split('<')[0]
  54. hop_tag = dom.getElementsByTagName('hop')[0].toxml()
  55. hop_data = hop_tag.replace('<hop>','').replace('</hop>','')
  56. host_tag = dom.getElementsByTagName('host')[0].toxml()
  57. host_data = host_tag.replace('<host>','').replace('</host>','')
  58. hop_ip_tag = dom.getElementsByTagName('hop_ip')[0].toxml()
  59. hop_ip_data = hop_ip_tag.replace('<hop_ip>','').replace('</hop_ip>','')
  60. longitude_tag = dom.getElementsByTagName('longitude')[0].toxml()
  61. longitude_data = longitude_tag.replace('<longitude>','').replace('</longitude>','')
  62. latitude_tag = dom.getElementsByTagName('latitude')[0].toxml()
  63. latitude_data = hop_tag.replace('<latitude>','').replace('</latitude>','')
  64. city_tag = dom.getElementsByTagName('city')[0].toxml()
  65. city_data = city_tag.replace('<city>','').replace('</city>','')
  66. country_tag = dom.getElementsByTagName('country')[0].toxml()
  67. country_data = country_tag.replace('<country>','').replace('</country>','')
  68. server_name_tag = dom.getElementsByTagName('server_name')[0].toxml()
  69. server_name_data = server_name_tag.replace('<server_name>','').replace('</server_name>','')
  70. asn_tag = dom.getElementsByTagName('asn')[0].toxml()
  71. asn_data = asn_tag.replace('<asn>','').replace('</asn>','')
  72. timestamp_tag = dom.getElementsByTagName('timestamp')[0].toxml()
  73. timestamp_data = timestamp_tag.replace('<timestamp>','').replace('</timestamp>','')
  74. country_code_tag = dom.getElementsByTagName('country_code')[0].toxml()
  75. country_code_data = country_code_tag.replace('<country_code>','').replace('</country_code>','')
  76. meta_tag = dom.getElementsByTagName('meta')[0].toxml()
  77. meta_data = meta_tag.replace('<meta>','').replace('</meta>','')
  78. return travel_data, hop_data, hop_ip_data, longitude_data, latitude_data, city_data, country_data, server_name_data, asn_data, timestamp_data, country_code_data, meta_data