Created
June 10, 2022 22:20
-
-
Save MrJeremyHobbs/6174d78d32413f26a9983dbd1e25885e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/python3 | |
import os | |
import csv | |
from ipwhois import IPWhois | |
# clean-up | |
try: | |
os.remove('.\\output\\Z39.50 Usage - IPs (last 3 months) -- enhanced.csv') | |
except FileNotFoundError: | |
pass | |
# open input CSV and loop through | |
with open('.\\input\Z39.50 Usage - IPs (last 3 months).csv', | |
'r', encoding="utf-8") as csv_file: | |
reader = csv.reader(csv_file) | |
# skip header | |
next(reader) | |
# parse CSV data from Alma analytics | |
for row in reader: | |
ip = row[3] | |
name = "" | |
description = "" | |
country = "" | |
try: | |
obj=IPWhois(ip) | |
results = obj.lookup_whois() | |
description = results['nets'][0]['description'] | |
name = results['nets'][0]['name'] | |
country = results['nets'][0]['country'] | |
row.append(name) | |
row.append(description) | |
row.append(country) | |
except Exception as e: | |
#print(e) | |
row.append(name) | |
row.append(description) | |
row.append(country) | |
# write new CSV | |
with open('.\\output\\Z39.50 Usage - IPs (last 3 months) -- enhanced.csv', 'a', encoding="utf-8", newline="") as outfile: | |
writer = csv.writer(outfile) | |
writer.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment