#!/usr/bin/python3
# import argparse library to handle and parse user input.
import argparse
# import requests library to handle http requests.
import requests
def get_headers(infile):
# get_headers will attempt to make a request to each url and return results
header_info = []
for url in infile:
http_req = None
# attempt to make a connection if not add the error to the list.
if not url:
continue
try:
http_req = requests.get(url.strip())
except Exception as error:
header_info.append('################################')
header_info.append("error: {0}".format(error))
header_info.append('################################')
# if no results next url else add information to list.
if not http_req:
continue
# format the information for human readablity.
header_info.append('--------------------------------')
header_info.append(url.strip())
for items in http_req.headers:
header_info.append('{0}: {1}'.format(items, http_req.headers[items]))
#close out connect before making the next.
http_req.close()
# return the information gathered.
return header_info
# check to see the script is being ran independently, if so run the parser for user input.
if __name__ == '__main__':
# set up argument parser and define inputs.
parser = argparse.ArgumentParser(description='simple banner grabber')
parser.add_argument('-i', '--input_file', type=argparse.FileType('r'), help='The file containing urls to requests headers from', required=True)
parser.add_argument('-o', '--output_file', help='The file to save the results to')
# get user input.
args = parser.parse_args()
# get the headers utilzing the get_headers function.
headers = get_headers(args.input_file)
# display the output
for lines in headers:
print(lines)
# check to see if the user wanted the data saved and do it
if args.output_file:
with open(args.output_file, "w") as savefile:
for lines in headers:
savefile.write(lines)
print('################################')
print("Saved out put to {0}".format(args.output_file))