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.

74 lines
2.0 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. main_api = 'http://ip-api.com/json/'
  9. # For every host do an API request
  10. try:
  11. for x in host:
  12. json_data = requests.get(main_api + x).json()
  13. if 'message' in json_data:
  14. print('\nThe IP "{}" is {}'.format(x,
  15. json_data['message']))
  16. # Print out wanted JSON data formatted nicely
  17. else:
  18. print('\nCity\State: {}, {}\n'
  19. 'Country: {}\n'
  20. 'ISP: {}\n'
  21. 'IP: {}\n'
  22. 'MX: {}'.format(
  23. json_data['city'],
  24. json_data['regionName'],
  25. json_data['country'],
  26. json_data['isp'],
  27. json_data['query'],
  28. x))
  29. except KeyError:
  30. traceback.print_exc(file=sys.stdout)
  31. print('Key Error')
  32. print('JSON: ')
  33. print(json_data)
  34. def findMX(host):
  35. p = os.popen('host -t MX ' + host)
  36. # initialize dicts
  37. std_out = []
  38. split = []
  39. MXServer = []
  40. # append terminal output to variable std_out
  41. for line in p:
  42. if re.search('not found', line):
  43. query_api([host])
  44. break
  45. elif re.search('domain name pointer', line):
  46. query_api([host])
  47. extra = re.search('.in-addr.arpa .*',str(line))
  48. thing = line.replace(extra.group(0), '')
  49. query_api([thing.rstrip()])
  50. break
  51. std_out.append(line)
  52. p.close
  53. # create iterator
  54. i = 0
  55. # split line into dict and return MX servers
  56. for x in std_out:
  57. split = std_out[i].split()
  58. MXServer.append(split[-1])
  59. i = i + 1
  60. query_api(MXServer)
  61. if __name__ == "__main__":
  62. parser = argparse.ArgumentParser()
  63. parser.add_argument("host", help="hostname to lookip")
  64. args = parser.parse_args()
  65. findMX(args.host)