Skip to content

Instantly share code, notes, and snippets.

@pkutzner
Last active November 24, 2019 16:29
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 pkutzner/9accbfced6647097eb15f05d97e03dcc to your computer and use it in GitHub Desktop.
Save pkutzner/9accbfced6647097eb15f05d97e03dcc to your computer and use it in GitHub Desktop.
Shell script to get public Google Cloud IP ranges
#!/usr/bin/env bash
#set -x
#trap read debug
URL="_cloud-netblocks.googleusercontent.com"
get_ranges() {
local includes=($(host -t TXT "$1"| egrep -o "include:_cloud-[^ ]*" | cut -d':' -f2))
local ranges=($(host -t TXT "$1" | egrep -o "ip4:((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?/([0-9]?[1-3][0-9]))" | cut -d':' -f2))
#echo "${ranges[@]}"
[ ! -z $ranges ] && printf '%s\n' "${ranges[@]}"
if [ ! -z $includes ]; then
for include in "${includes[@]}"; do
get_ranges "$include"
done
fi
}
get_ranges "$URL" | sort -t'.' -n -k1,1 -k2,2 -k3,3 -k4,4 | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment