Added some comments for readability
This commit is contained in:
parent
993e6feff0
commit
ac8980c614
15
ip-api.py
15
ip-api.py
@ -7,14 +7,17 @@ import traceback
|
|||||||
|
|
||||||
|
|
||||||
def query_api(host):
|
def query_api(host):
|
||||||
|
"""Queries the ip-api site in order to check geolocation and mx record of
|
||||||
|
the host"""
|
||||||
main_api = 'http://ip-api.com/json/'
|
main_api = 'http://ip-api.com/json/'
|
||||||
# For every host do an API request
|
# For every host do an API request
|
||||||
try:
|
try:
|
||||||
for x in host:
|
for x in host:
|
||||||
json_data = requests.get(main_api + x).json()
|
json_data = requests.get(main_api + x).json()
|
||||||
|
# Checks to see if there is a 'message' field in the json data and
|
||||||
|
# prints the message instead of doing a query
|
||||||
if 'message' in json_data:
|
if 'message' in json_data:
|
||||||
print('\nThe IP "{}" is {}'.format(x,
|
print('\nThe IP "{}" is {}'.format(x, json_data['message']))
|
||||||
json_data['message']))
|
|
||||||
# Print out wanted JSON data formatted nicely
|
# Print out wanted JSON data formatted nicely
|
||||||
else:
|
else:
|
||||||
print('\nCity\State: {}, {}\n'
|
print('\nCity\State: {}, {}\n'
|
||||||
@ -28,6 +31,8 @@ def query_api(host):
|
|||||||
json_data['isp'],
|
json_data['isp'],
|
||||||
json_data['query'],
|
json_data['query'],
|
||||||
x))
|
x))
|
||||||
|
# Added exception handling of key errors to help identify problems when
|
||||||
|
# reading the json data
|
||||||
except KeyError:
|
except KeyError:
|
||||||
traceback.print_exc(file=sys.stdout)
|
traceback.print_exc(file=sys.stdout)
|
||||||
print('Key Error')
|
print('Key Error')
|
||||||
@ -36,6 +41,7 @@ def query_api(host):
|
|||||||
|
|
||||||
|
|
||||||
def findMX(host):
|
def findMX(host):
|
||||||
|
"""Looks up the MX record of a host"""
|
||||||
p = os.popen('host -t MX ' + host)
|
p = os.popen('host -t MX ' + host)
|
||||||
|
|
||||||
# initialize dicts
|
# initialize dicts
|
||||||
@ -48,9 +54,12 @@ def findMX(host):
|
|||||||
if re.search('not found', line):
|
if re.search('not found', line):
|
||||||
query_api([host])
|
query_api([host])
|
||||||
break
|
break
|
||||||
|
# Checs to see if 'domain name pointer' is in the line and finds the ip
|
||||||
|
# associated with the pointer to do a query on. Created for IPs that do
|
||||||
|
# not have a easily parsed MX record return.
|
||||||
elif re.search('domain name pointer', line):
|
elif re.search('domain name pointer', line):
|
||||||
query_api([host])
|
query_api([host])
|
||||||
extra = re.search('.in-addr.arpa .*',str(line))
|
extra = re.search('.in-addr.arpa .*', str(line))
|
||||||
thing = line.replace(extra.group(0), '')
|
thing = line.replace(extra.group(0), '')
|
||||||
query_api([thing.rstrip()])
|
query_api([thing.rstrip()])
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user