Azure NSG Dump

De Wiki Clusterlab.com.br
Ir para navegação Ir para pesquisar

First log in using azure-cli.
The script will dump all nsgs in all subscriptions the users is subscribed to json files. One per subscription
You may run it from Azure cloud shell.

#!/bin/bash
export DATETIME=$(date +"%Y-%m-%d_%H-%M_%S")
function ARQTEMP() {
case $1 in
        criar)
                mktemp -t tempfile.XXXXXX
                ;;
        apagar)
                rm -fr $2
                ;;
esac
}
function GETTOKEN() {
  echo get TOKEN
  # curl -X POST -d 'grant_type=client_credentials&client_id=[APP_ID]&client_secret=[PASSWORD]&resource=https%3A%2F%2Fmanagement.azure.com%2F' https://login.microsoftonline.com/[TENANT_ID]/oauth2/token
}
# https://management.azure.com/subscriptions/<xxxxxxxxxxxxxxxx>/providers/Microsoft.Network/networkSecurityGroups?api-version=2018-07-01
function GETDATA() {
  curl -X GET \
  -H "Authorization: Bearer $2" \
  -H "Content-Type: application/json" https://management.azure.com/subscriptions/$1/providers/Microsoft.Network/networkSecurityGroups?api-version=2018-07-01
}
export ACCOUNTDATA=$(ARQTEMP criar)
az account get-access-token  -o tsv > $ACCOUNTDATA
export ACCESSTOKEN=$(cat $ACCOUNTDATA | awk '{print $1}')
export ACCESSTENANT=$(cat $ACCOUNTDATA | awk '{print $3}')
export ACCESSSUBSCRIPTION=$(cat $ACCOUNTDATA | awk '{print $2}')
ARQTEMP apagar $ACCOUNTDATA
az account list --query "[].[id]" -o tsv | while read SUBSCRIPTION
do
  GETDATA $SUBSCRIPTION $ACCESSTOKEN > $DATETIME-$SUBSCRIPTION.json
done