You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.6 KiB

  1. import argparse
  2. import requests
  3. import os
  4. import re
  5. import sys
  6. import traceback
  7. def query_api(host):
  8. """Queries the ip-api site in order to check geolocation and mx record of
  9. the host"""
  10. main_api = 'http://ip-api.com/json/'
  11. # For every host do an API request
  12. try:
  13. for x in host:
  14. json_data = requests.get(main_api + x).json()
  15. # Checks to see if there is a 'message' field in the json data and
  16. # prints the message instead of doing a query
  17. if 'message' in json_data:
  18. print('\nThe IP "{}" is {}'.format(x, json_data['message']))
  19. # Print out wanted JSON data formatted nicely
  20. else:
  21. print('\nAS: {}\n'
  22. 'City\State: {}, {}\n'
  23. 'Country: {}\n'
  24. 'ISP: {}\n'
  25. 'IP: {}\n'
  26. 'MX: {}'.format(
  27. json_data['as'],
  28. json_data['city'],
  29. json_data['regionName'],
  30. json_data['country'],
  31. json_data['isp'],
  32. json_data['query'],
  33. x))
  34. # Added exception handling of key errors to help identify problems when
  35. # reading the json data
  36. except KeyError:
  37. traceback.print_exc(file=sys.stdout)
  38. print('Key Error')
  39. print('JSON: ')
  40. print(json_data)
  41. def findMX(host):
  42. """Looks up the MX record of a host"""
  43. p = os.popen('host -t MX ' + host)
  44. # initialize dicts
  45. std_out = []
  46. split = []
  47. MXServer = []
  48. # append terminal output to variable std_out
  49. for line in p:
  50. if re.search('not found', line):
  51. print('No MX record found querying ' + host)
  52. query_api([host])
  53. break
  54. # Checks to see if 'domain name pointer' is in the line and finds the
  55. # ip associated with the pointer to do a query on. Created for IPs that
  56. # do not have a easily parsed MX record return.
  57. elif re.search('domain name pointer', line):
  58. print(line)
  59. print('Domain name pointer found querying original host: ' + host)
  60. query_api([host])
  61. extra = re.search('.in-addr.arpa .*', str(line))
  62. thing = line.replace(extra.group(0), '')
  63. print('\nDomain Name pointer Query: ' + thing)
  64. query_api([thing.rstrip()])
  65. break
  66. std_out.append(line)
  67. p.close
  68. # split line into dict and return MX servers
  69. for x in std_out:
  70. # When using os.popen it basically acts like a terminal allowing you to
  71. # run terminal commands from your Python script and use its output. We
  72. # are using as an example 'host -t MX google.com' the output would look
  73. # like:
  74. # google.com mail is handled by 30 alt2.aspmx.l.google.com
  75. # google.com mail is handled by 40 alt3.aspmx.l.google.com
  76. # google.com mail is handled by 10 aspmx.l.google.com
  77. # google.com mail is handled by 20 alt1.aspmx.l.google.com
  78. # google.com mail is handled by 50 alt4.aspmx.l.google.com
  79. split = std_out[x].split()
  80. # We use .split() method to split the std_out list entry by spaces
  81. MXServer.append(split[-1])
  82. # We take the last item in the split(aspmx.l.google.com) and append it
  83. # to the list 'MXServer'
  84. query_api(MXServer)
  85. # Now we send the list 'MXServer' to the query_api function
  86. if __name__ == "__main__":
  87. parser = argparse.ArgumentParser()
  88. parser.add_argument("host", help="hostname to lookip")
  89. args = parser.parse_args()
  90. findMX(args.host)