Skip to content

Instantly share code, notes, and snippets.

@Azlirn
Last active April 17, 2019 23:39
Show Gist options
  • Save Azlirn/4a043cd80b1e0bdd9b65f1a1a0bb6773 to your computer and use it in GitHub Desktop.
Save Azlirn/4a043cd80b1e0bdd9b65f1a1a0bb6773 to your computer and use it in GitHub Desktop.
This short Python script uses the Free ipinfo.io API to gather information for a provided list of IP Address and write the results to a separate .csv file.
import csv
import ipinfo
import re
def main():
with open('data.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
details = getDetails(row['IPAddress'])
writeDetails(details)
def getDetails(ipAddress):
token = '' # INSERT YOUR IPINFO API TOKEN HERE
handler = ipinfo.getHandler(token)
print('\n[*] Grabbing Details for %s' % ipAddress)
try:
details = handler.getDetails(ipAddress)
print(details.all)
return details
except Exception as e:
print('[!] Exception Occurred --> %s | Ignoring...\n' % e)
pass
def writeDetails(details):
'''
Example Output from API:
{'ip': '181.215.139.106', 'city': 'Elk Grove Village', 'region': 'Illinois', 'country': 'US',
'loc': '42.0039,-87.9703', 'postal': '60007', 'phone': '847', 'org': 'AS61317 Digital Energy Technologies Ltd.',
'country_name': 'United States', 'ip_address': IPv4Address('181.215.139.106'), 'latitude': '42.0039',
'longitude': '-87.9703'}
'''
ip = details.ip
# city = details.city
# region = details.region
# country = details.country
# geoLocation = details.loc
# zipCode = details.postal
# phoneAreaCode = details.phone
organization = details.org
countryName = details.country_name
# ipAddressType = details.ip_address
# lattitude = details.latitude
# longitude = details.longitude
asn = extractASN(details)
dataToWrite = [ip, countryName, organization, asn]
with open('newData.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(dataToWrite)
def extractASN(details):
string = details.org
asnSearch = re.search('(AS[0-9]*)', string)
if asnSearch:
asn = asnSearch.group(1)
return asn
else:
return None
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment