Skip to content

Instantly share code, notes, and snippets.

@rproenca
Created October 13, 2020 20:01
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 rproenca/229a8651bb7fc9381dc3342186ec6280 to your computer and use it in GitHub Desktop.
Save rproenca/229a8651bb7fc9381dc3342186ec6280 to your computer and use it in GitHub Desktop.
A bash script that generates a list of CIDRs given a Azure IP Ranges JSON file
#!/bin/bash
# Generates a list of CIDRs given an Azure IP Ranges JSON file
# https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519
INPUT_FILENAME=$1; # Name of the JSON file downloaded from MS website (ServiceTags_Public_YYYYMMDD.json)
OUTPUT_FILENAME=cidr_list.txt
if [ -z $INPUT_FILENAME ]
then
printf "Incorrect usage. Please use: ./gen_cidr_list.sh [json_file]\n";
printf "Example: ./gen_cidr.sh ServiceTags_Public_20201012.json\n";
else
# Help:
#
# .values[]
# extract everything from the "values" array
#
# select(.id == "AzureCloud.brazilsouth").properties.addressPrefixes
# get only IP addresses from Brazil South Azure Cloud
#
# .[]
# flat the result into simple strings
#
# select(. | test("^([0-9]{1,3}\\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$"))
# select only IPs in IPV4 CIDR format
#
jq --raw-output '.values[] | select(.id == "AzureCloud.brazilsouth").properties.addressPrefixes | .[] | select(. | test("^([0-9]{1,3}\\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$"))' $INPUT_FILENAME > $OUTPUT_FILENAME
echo "File created: $OUTPUT_FILENAME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment