Skip to content

Instantly share code, notes, and snippets.

@jsfan3
Created January 23, 2023 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsfan3/6460b3a7326687cd1af3958b0ef4b417 to your computer and use it in GitHub Desktop.
Save jsfan3/6460b3a7326687cd1af3958b0ef4b417 to your computer and use it in GitHub Desktop.
Counts IPv4 addresses in MaxMind's GeoLite2 database
#!/bin/bash
### Counts IPv4 addresses in MaxMind's GeoLite2 database ###
### It does the summation of how many IPs are in each CIDR range ###
### https://dev.maxmind.com/geoip/geolite2-free-geolocation-data ###
echo "Warning: the execution of this script can take many hours!"
param=0 # $param contains the summation
skip='true' # we use $skip to skip the first row, containing the CSV header row
file="GeoLite2-City-Blocks-IPv4.csv" # this is the database, it must be in the same folder as the script
while read line; do
if [[ $skip == 'true' ]]; then
skip='false';
continue; # we skip the CSV header row
fi;
num=`echo "${line}" | awk -F / '{ count[$2]++ } END { for (mask in count) total+=count[mask]*2^(32-mask); print total }'`
#echo "IPs added: $num"; # number of IPs in the CIDR; we can uncomment for debugging
param=$(bc <<< "$param + $num")
#echo "Total IPs: $param" # summation; we can uncomment for debugging
done < "${file}"
echo "Total IPv4 addresses: $param"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment