Skip to content

Instantly share code, notes, and snippets.

@Flecart
Created March 19, 2024 20:56
Show Gist options
  • Save Flecart/b165dbe4b77321f76ee3bcd942aed5eb to your computer and use it in GitHub Desktop.
Save Flecart/b165dbe4b77321f76ee3bcd942aed5eb to your computer and use it in GitHub Desktop.
Give a CSV with the list of IPs, then this script will try to create a map with the geolocation of everyIP! Don't know if you can batch the requests, currectly it is quite slow, at a rate of 30 plots for minute.
import pandas as pd
from geopy.geocoders import Nominatim
import folium
# Load the CSV file with pandas
df = pd.read_csv('access-log.csv') # Replace 'ip_addresses.csv' with your CSV file name
filtered_df = df[~df['Source IP'].str.startswith('192.168.1.')]
# Initialize geocoder
geolocator = Nominatim(user_agent="geoapiExercises")
# Create a folium map centered around the first IP's location
first_ip = filtered_df['Source IP'].iloc[0] # Assuming the first column in the CSV is 'source_ip'
location = geolocator.geocode(first_ip)
mymap = folium.Map(location=[location.latitude, location.longitude], zoom_start=10)
# Add markers for each IP address
for ip in df['Source IP']:
location = geolocator.geocode(ip)
if location:
folium.Marker([location.latitude, location.longitude], popup=ip).add_to(mymap)
# Save the map to an HTML file
mymap.save("ip_locations_map.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment