Last active
April 23, 2023 07:45
-
-
Save westh/64d7c1cebb7972b734588514927ab2f7 to your computer and use it in GitHub Desktop.
Enrich Cloud/Hosted Grafana IPs with country codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
ips=$(curl -s https://grafana.com/api/hosted-metrics/source-ips.txt) | |
declare -A country_code_to_ip_map | |
for ip in $ips; do | |
country=$(curl -s https://ipinfo.io/$ip/country) | |
country_code_to_ip_map[$country]="${country_code_to_ip_map[$country]} \"$ip\"" | |
done | |
echo "{" | |
for country_code in "${!country_code_to_ip_map[@]}"; do | |
formatted_and_sorted_ips=$(echo "${country_code_to_ip_map[$country_code]}" | sed 's/ //' | tr ' ' '\n' | sort | tr '\n' ',' | sed 's/,$//g') | |
echo "\"$country_code\": [$formatted_and_sorted_ips]," | |
done | sed '$s/,$//' | |
echo "}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So if you run:
You'll get something like this (well the IPs might change over time, this was run 2023-04-23T09:40:04Z):
And if you only want e.g. the IPs that comes from the US then you can utilise
jq
's selection:Which will output: