Skip to content

Instantly share code, notes, and snippets.

@abdullahdevrel
Created June 20, 2024 20:57
Show Gist options
  • Save abdullahdevrel/eb77f3ee19cb01a1769782f86bb5f4fa to your computer and use it in GitHub Desktop.
Save abdullahdevrel/eb77f3ee19cb01a1769782f86bb5f4fa to your computer and use it in GitHub Desktop.
IPinfo map IPs using folium and add info on hover
import folium # pip install folium
import ipinfo # pip install ipinfo
# Initialize the IPinfo handler with the provided token
# https://ipinfo.io/account/token
token = ""
handler = ipinfo.getHandler(token)
# Read IP addresses from the file "ips.txt"
with open("ips.txt", "r") as f:
ips = f.read().splitlines()
# Fetch details for the list of IP addresses
ip_data = handler.getBatchDetails(ips)
# Dictionary to store IP points with their locations
ip_points = {}
# Iterate through the fetched IP data
for ip_point in ip_data.values():
# Skip bogon (private/internal) IP addresses
if "bogon" not in ip_point:
# If location is not already in the dictionary, add it
if ip_point['loc'] not in ip_points:
ip_points[ip_point['loc']] = []
# Append the IP address to the location
ip_points[ip_point['loc']].append(ip_point['ip'])
# Initialize a folium map centered at (0, 0) with a zoom level of 2
m = folium.Map(location=(0, 0), zoom_start=2)
# Add markers to the map for each location with the IP addresses
for key, value in ip_points.items():
folium.Marker(location=key.split(","), popup="\n".join(value)).add_to(m)
# Save the map to an HTML file
m.save("IPinfo_map.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment