Skip to content

Instantly share code, notes, and snippets.

@feczo
Created November 5, 2015 06:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feczo/eae831a08c98a94e6fd9 to your computer and use it in GitHub Desktop.
Save feczo/eae831a08c98a94e6fd9 to your computer and use it in GitHub Desktop.
BLOCKS=$(nslookup -q=TXT _cloud-netblocks.googleusercontent.com 8.8.8.8 | awk -F "spf1 " '{print $2}' | sed -e "s/include:/\n/g" | sed -e "s/?all\"//g" | grep -v ^$)
echo "$BLOCKS" | while read block; do nslookup -q=TXT $block 8.8.8.8 | awk -F "spf1 " '{print $2}' | sed -e "s/ip4:/\n/g" | sed -e "s/?all\"//g" |grep -v "^ip6:" | grep -v ^$; done ;
@feczo
Copy link
Author

feczo commented Nov 5, 2015

based on https://cloud.google.com/compute/docs/faq#ipranges

example output:
8.34.208.0/20
8.35.192.0/21
8.35.200.0/23
108.59.80.0/20
108.170.192.0/20
108.170.208.0/21
108.170.216.0/22
108.170.220.0/23
108.170.222.0/24
162.216.148.0/22
162.222.176.0/21
173.255.112.0/20
192.158.28.0/22
199.192.112.0/22
199.223.232.0/22
199.223.236.0/23
23.236.48.0/20
23.251.128.0/19
107.167.160.0/19
107.178.192.0/18
146.148.2.0/23
146.148.4.0/22
146.148.8.0/21
146.148.16.0/20
146.148.32.0/19
146.148.64.0/18
130.211.4.0/22
130.211.8.0/21
130.211.16.0/20
130.211.32.0/19
130.211.64.0/18
130.211.128.0/17
104.154.0.0/15
104.196.0.0/14

@Civil
Copy link

Civil commented Jul 2, 2018

Nowadays includes might also contain other includes. (e.x. netblocks1 contains include:_cloud-netblocks6.googleusercontent.com) so you need to do recursive query. A script that can do recursive queries will look like that (might also need some improvements though):

#!/bin/bash
resolve_include() {
    BLOCK="${1}"
    RES=$(nslookup -q=TXT ${BLOCK} 8.8.8.8 | awk -F "spf1 " '{print $2}' | sed -e "s/ip4:/\n/g;s/?all\"//g" | grep -v "^ip6:" | grep -v ^$)
    echo "${RES}"
}

RES="_cloud-netblocks.googleusercontent.com"
cont=true
while [[ ${cont} != "false" ]]; do
    NEW_RES=""
    CNT=0
    for r in ${RES}; do
        blocks="${r}"
        grep -q "include" <<< "${r}" && r=$(cut -d: -f 2 <<< "${r}")
        grep -q "googleusercontent.com$" <<< "${r}" && { blocks="$(resolve_include ${r})"; CNT=$((CNT+1)); }
        NEW_RES="${NEW_RES} ${blocks}"
    done
    if [[ ${CNT} -eq 0 ]]; then
        cont="false"
    fi
    RES="${NEW_RES}"
done
echo "${RES}" | sed 's/ /\n/g' | sort -u -n

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