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.

57 lines
1.4 KiB

  1. import argparse
  2. import requests
  3. import os
  4. import re
  5. def query_api(host):
  6. main_api = 'http://ip-api.com/json/'
  7. # For every host do an API request
  8. for x in host:
  9. json_data = requests.get(main_api + x).json()
  10. # Print out wanted JSON data formatted nicely
  11. print('\nCity\State: {}, {}\n'
  12. 'Country: {}\n'
  13. 'ISP: {}\n'
  14. 'IP: {}\n'
  15. 'MX: {}'.format(
  16. json_data['city'],
  17. json_data['regionName'],
  18. json_data['country'],
  19. json_data['isp'],
  20. json_data['query'],
  21. x))
  22. def findMX(host):
  23. p = os.popen('host -t MX ' + host)
  24. # initialize dicts
  25. std_out = []
  26. split = []
  27. MXServer = []
  28. # append terminal output to variable std_out
  29. for line in p:
  30. std_out.append(line)
  31. p.close
  32. # create iterator
  33. i = 0
  34. # split line into dict and return MX servers
  35. for x in std_out:
  36. split = std_out[i].split()
  37. MXServer.append(split[-1])
  38. i = i + 1
  39. query_api(MXServer)
  40. if __name__ == "__main__":
  41. parser = argparse.ArgumentParser()
  42. parser.add_argument("host", help="hostname to lookip")
  43. args = parser.parse_args()
  44. # If the arg is an IP address go straight to API
  45. if re.search('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', args.host):
  46. query_api([args.host])
  47. else:
  48. findMX(args.host)