├── README.md ├── ipFile.txt ├── locateIPs.py └── nmapLocateIps.py /README.md: -------------------------------------------------------------------------------- 1 | # ip-locator 2 | This python script generates a Google Earth file from a text file of IP Addresses. The script also generates a geo-stats.csv file with information about the IPs. 3 | IP information includes latitude, longitude, city, region, country, ISP, and if the IP belongs to a VPN provider. 4 | 5 | ## Steps 6 | Install the python libraries using pip3: 7 |
8 | pip3 install requests 9 | pip3 install simplekml 10 | pip3 install csv 11 |12 | 13 | Run the script 14 | 15 |
16 | chmod +x locateIPs.py 17 | ./locateIPs.py18 | 19 | ## Optional VPN and ISP Detection 20 | 21 | Steps: 22 |
23 | 1. Register for an account on the website https://iphub.info/. 24 | 2. Generate a free API key. This will let you make 1000 requests a day. 25 | 3. Make a file called iphub-info.txt within the locateIPs.py directory. 26 | 4. Paste the API key in the file. 27 |28 | 29 | ## How-to Video 30 | [](https://www.youtube.com/watch?v=IEl-kBQvutU) 31 | -------------------------------------------------------------------------------- /ipFile.txt: -------------------------------------------------------------------------------- 1 | 148.72.171.189 2 | 92.119.18.43 3 | 192.145.117.176 4 | 23.105.165.49 5 | 64.44.55.29 6 | 37.19.213.69 7 | 192.145.119.51 8 | 23.81.112.203 9 | 23.82.194.114 10 | 192.145.80.86 11 | -------------------------------------------------------------------------------- /locateIPs.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import os.path 4 | import requests 5 | import json 6 | import csv 7 | import simplekml 8 | 9 | arGeoResults = [] 10 | bVPNApi = False 11 | strAPIKey = "" 12 | 13 | if os.path.exists("ipFile.txt") == False: 14 | print("Error. Please create a file called ipFile.txt and supply IP Addresses separated by new line breaks.") 15 | exit() 16 | 17 | if os.path.exists("iphub-info.txt") == True: 18 | bVPNApi = True 19 | strAPIKey = open("iphub-info.txt").readline().strip() 20 | print("iphub-info.txt API key exists. API will be used.") 21 | 22 | def getVPNStatus(ip): 23 | strLink = "http://v2.api.iphub.info/ip/" + ip.strip() 24 | headers={"X-Key" : strAPIKey} 25 | response = requests.get(strLink, headers=headers, auth=None) 26 | strResponse = response.content.decode("utf-8") 27 | jsonResponse = json.loads(strResponse) 28 | return jsonResponse 29 | 30 | 31 | ipFile = open("ipFile.txt") 32 | for ip in ipFile: 33 | print("IP Address: " + ip.strip()) 34 | response = requests.get("http://www.geoplugin.net/json.gp?ip=" + ip) 35 | strResponse = response.content.decode("utf-8") 36 | jsonResponse = json.loads(strResponse) 37 | jsonResponse["isp"] = ""; 38 | jsonResponse["block"] = ""; 39 | if (response.status_code == 200): 40 | arGeoResults.append(jsonResponse) 41 | if (bVPNApi == True): 42 | jsonVPN = getVPNStatus(ip) 43 | jsonResponse["isp"] = jsonVPN["isp"] 44 | jsonResponse["block"] = jsonVPN["block"] 45 | else: 46 | print(ip + " had an error. Status Code " + response.status_code) 47 | 48 | geoFile = open("geo-coords.csv", "w") 49 | statsFile = open("geo-stats.csv", "w") 50 | strStatsTitle = "IP Address, Latitude, Longitude, City, Region, Country, Timezone, ISP, Blocked\n" 51 | statsFile.write(strStatsTitle) 52 | 53 | for jsonGeo in arGeoResults: 54 | strGeoTitle = '"' + jsonGeo["geoplugin_request"].strip() + ' ' + jsonGeo["geoplugin_city"] + '"'; 55 | strGeo = strGeoTitle + ',' + jsonGeo["geoplugin_latitude"] + ',' + jsonGeo["geoplugin_longitude"] 56 | strStats = jsonGeo["geoplugin_request"].strip() + ',' + jsonGeo["geoplugin_latitude"] + ',' + jsonGeo["geoplugin_longitude"] + ',' + \ 57 | jsonGeo["geoplugin_city"] + ',' + jsonGeo["geoplugin_region"] + ',' + jsonGeo['geoplugin_countryName'] + ',' + \ 58 | jsonGeo["geoplugin_timezone"] + ',' + jsonGeo["isp"] + ',' + str(jsonGeo["block"]) 59 | print(strGeo) 60 | geoFile.write(strGeo + "\n") 61 | statsFile.write(strStats + "\n") 62 | 63 | geoFile.close() 64 | statsFile.close() 65 | 66 | locationFile = csv.reader(open('geo-coords.csv','r')) 67 | kml=simplekml.Kml() 68 | 69 | for row in locationFile: 70 | kml.newpoint(name=row[0], coords=[(row[2],row[1])]) 71 | 72 | kml.save('locations.kml') 73 | print("Success! locations.xml and geo-status.csv have been created.") 74 | -------------------------------------------------------------------------------- /nmapLocateIps.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import os.path 4 | import subprocess 5 | import csv 6 | import simplekml 7 | 8 | arNmapResults = [] 9 | arIP = []; 10 | 11 | if os.path.exists("ipFile.txt") == False: 12 | print("Error. Please create a file called ipFile.txt and supply IP Addresses separated by new line breaks.") 13 | exit() 14 | 15 | ipFile = open("ipFile.txt") 16 | for ip in ipFile: 17 | print("IP Address: " + ip.strip()) 18 | args = ["nmap", "-sn", "-Pn", "--script", "ip-geolocation-geoplugin", ip.strip()] 19 | p = subprocess.Popen(args, stdout=subprocess.PIPE) 20 | stdout, stderr = p.communicate() 21 | strAnswer = str(stdout, "utf-8") 22 | arNmapResults.append(strAnswer) 23 | arIP.append(ip.strip()) 24 | #print(strAnswer) 25 | 26 | ipCounter = 0 27 | csvFile = open("geo-coords.csv", "w") 28 | 29 | for lineNmap in arNmapResults: 30 | for line in lineNmap.split("\n"): 31 | if ("coordinates" in line): 32 | #print("COORDS:" + line) 33 | strCSV = arIP[ipCounter] + ',' + line.split("coordinates:")[1] 34 | print(strCSV) 35 | csvFile.write(strCSV + "\n") 36 | ipCounter += 1 37 | 38 | csvFile.close() 39 | 40 | 41 | locationFile = csv.reader(open('geo-coords.csv','r')) 42 | kml=simplekml.Kml() 43 | 44 | for row in locationFile: 45 | kml.newpoint(name=row[0], coords=[(row[2],row[1])]) 46 | 47 | kml.save('locations.kml') 48 | --------------------------------------------------------------------------------