Skip to content

Instantly share code, notes, and snippets.

@westh
Last active April 23, 2023 07:45
Show Gist options
  • Save westh/64d7c1cebb7972b734588514927ab2f7 to your computer and use it in GitHub Desktop.
Save westh/64d7c1cebb7972b734588514927ab2f7 to your computer and use it in GitHub Desktop.
Enrich Cloud/Hosted Grafana IPs with country codes
#!/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 "}"
@westh
Copy link
Author

westh commented Apr 23, 2023

So if you run:

$ curl -s https://gist.githubusercontent.com/westh/64d7c1cebb7972b734588514927ab2f7/raw/419dd0c52d308799966f7d9ab3f78150f5f6cf9d/hosted-grafana-with-country-code.sh | bash -s | jq

You'll get something like this (well the IPs might change over time, this was run 2023-04-23T09:40:04Z):

{
  "BE": [
    "35.205.66.63"
  ],
  "GB": [
    "35.197.209.116"
  ],
  "DE": [
    "20.79.211.44",
    "3.69.125.237"
  ],
  "NL": [
    "20.31.17.143"
  ],
  "SG": [
    "18.139.155.52"
  ],
  "US": [
    "3.139.147.53",
    "34.102.140.197",
    "34.102.202.235",
    "34.102.237.169",
    "34.111.127.201",
    "34.111.145.147",
    "34.111.185.173",
    "34.111.203.20",
    "34.111.73.82",
    "34.117.103.153",
    "34.117.114.25",
    "34.117.118.190",
    "34.117.60.35",
    "34.117.67.173",
    "34.117.7.29",
    "34.120.115.151",
    "34.120.224.248",
    "34.120.72.239",
    "34.149.133.46",
    "34.149.19.234",
    "34.149.51.89",
    "34.149.81.97",
    "34.160.168.21",
    "34.216.126.36",
    "34.96.126.110",
    "35.186.222.79",
    "35.199.181.56",
    "35.201.117.100",
    "35.226.123.231",
    "35.237.114.245",
    "35.241.21.129",
    "35.244.146.53",
    "52.189.70.155"
  ],
  "CA": [
    "99.79.11.143"
  ]
}

And if you only want e.g. the IPs that comes from the US then you can utilise jq's selection:

$ curl -s https://gist.githubusercontent.com/westh/64d7c1cebb7972b734588514927ab2f7/raw/419dd0c52d308799966f7d9ab3f78150f5f6cf9d/hosted-grafana-with-country-code.sh | bash -s | jq '.US'

Which will output:

[
  "3.139.147.53",
  "34.102.140.197",
  "34.102.202.235",
  "34.102.237.169",
  "34.111.127.201",
  "34.111.145.147",
  "34.111.185.173",
  "34.111.203.20",
  "34.111.73.82",
  "34.117.103.153",
  "34.117.114.25",
  "34.117.118.190",
  "34.117.60.35",
  "34.117.67.173",
  "34.117.7.29",
  "34.120.115.151",
  "34.120.224.248",
  "34.120.72.239",
  "34.149.133.46",
  "34.149.19.234",
  "34.149.51.89",
  "34.149.81.97",
  "34.160.168.21",
  "34.216.126.36",
  "34.96.126.110",
  "35.186.222.79",
  "35.199.181.56",
  "35.201.117.100",
  "35.226.123.231",
  "35.237.114.245",
  "35.241.21.129",
  "35.244.146.53",
  "52.189.70.155"
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment