Last active
November 24, 2019 16:29
-
-
Save pkutzner/9accbfced6647097eb15f05d97e03dcc to your computer and use it in GitHub Desktop.
Shell script to get public Google Cloud IP ranges
This file contains hidden or 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 | |
#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