Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Forked from vulkoingim/gcp_ips.sh
Last active October 15, 2019 17:17
Show Gist options
  • Select an option

  • Save kgriffs/0a3c88e30bc36876ef9094642d71e591 to your computer and use it in GitHub Desktop.

Select an option

Save kgriffs/0a3c88e30bc36876ef9094642d71e591 to your computer and use it in GitHub Desktop.

Revisions

  1. kgriffs revised this gist Oct 15, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gcp_ips.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # the google cloud platform, according to ns-lookup / dig
    # TXT _cloud-netblocks.googleusercontent.com
    #
    # https://cloud.google.com/compute/docs/faq#ipranges
    # https://cloud.google.com/compute/docs/faq#find_ip_range

    errcho() {
    >&2 echo "$@"
  2. @vulkoingim vulkoingim created this gist May 24, 2018.
    59 changes: 59 additions & 0 deletions gcp_ips.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/usr/bin/env bash

    # This script lists all ip ranges currently used by
    # the google cloud platform, according to ns-lookup / dig
    # TXT _cloud-netblocks.googleusercontent.com
    #
    # https://cloud.google.com/compute/docs/faq#ipranges

    errcho() {
    >&2 echo "$@"
    }

    _fmt() {
    echo "$1" | cut -f "$2" -d':'
    }

    _txt_recursive() {
    for txt_entry in $(dig txt "$1" +short | tr " " "\n"); do
    if [[ "${txt_entry}" == include:* ]]; then
    _txt_recursive "$(_fmt ${txt_entry} 2)"
    elif [[ "${txt_entry}" == ip4:* && "${ipv4}" == true ]]; then
    _fmt "${txt_entry}" 2
    elif [[ "${txt_entry}" == ip6:* && "${ipv6}" == true ]]; then
    _fmt "${txt_entry}" 2-9
    fi
    done
    }

    domain="_cloud-netblocks.googleusercontent.com"

    ipv4=true
    ipv6=true

    while (( $# > 0 )); do
    case "$1" in
    --ipv4)
    ipv6=false
    shift 1
    ;;
    --ipv6)
    ipv4=false
    shift 1
    ;;
    --domain)
    domain="$2"
    shift 2
    ;;
    *)
    errcho "Unknown option $1"
    exit 1
    ;;
    esac
    done

    if [[ "${ipv4}" == false && "${ipv6}" == false ]]; then
    errcho "WARN: --ipv4 and --ipv6 options are mutually exclusive and will likely prevent output"
    fi

    _txt_recursive "${domain}"