Added regex to make entering IPs directly to work. Also added modifications to make it more in line iwht PEP8

This commit is contained in:
Michael Heidelberger 2018-04-19 10:04:44 -05:00
parent f1a4d29a8b
commit c1cc65d4ae

View File

@ -1,22 +1,27 @@
import argparse import argparse
import json
import requests import requests
import os import os
import re
def search(host):
def query_api(host):
main_api = 'http://ip-api.com/json/' main_api = 'http://ip-api.com/json/'
# For every host do an API request
ip = findMX(host) for x in host:
for host in ip: json_data = requests.get(main_api + x).json()
json_data = requests.get(main_api + host).json() # Print out wanted JSON data formatted nicely
print('\nCity\State: {}, {}\n'
print('\nCity\State: {}, {}\n Country: {}\n ISP: {}\n IP: {}\n MX: {}'.format( 'Country: {}\n'
'ISP: {}\n'
'IP: {}\n'
'MX: {}'.format(
json_data['city'], json_data['city'],
json_data['regionName'], json_data['regionName'],
json_data['country'], json_data['country'],
json_data['isp'], json_data['isp'],
json_data['query'], json_data['query'],
host)) x))
def findMX(host): def findMX(host):
p = os.popen('host -t MX ' + host) p = os.popen('host -t MX ' + host)
@ -36,13 +41,18 @@ def findMX(host):
# split line into dict and return MX servers # split line into dict and return MX servers
for x in std_out: for x in std_out:
split = std_out[i].split() split = std_out[i].split()
MXServer.append(split[6]) MXServer.append(split[-1])
i = i + 1 i = i + 1
return MXServer query_api(MXServer)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("host", help="hostname to lookip") parser.add_argument("host", help="hostname to lookip")
args = parser.parse_args() args = parser.parse_args()
# If the arg is an IP address go straight to API
search(args.host) if re.fullmatch(args.host,
'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'):
query_api(args.host)
else:
findMX(args.host)