Skip to content

Instantly share code, notes, and snippets.

@mainda01
Created May 7, 2024 11:39
Show Gist options
  • Select an option

  • Save mainda01/7955526c3fd7bc83f78f2c89348d38a7 to your computer and use it in GitHub Desktop.

Select an option

Save mainda01/7955526c3fd7bc83f78f2c89348d38a7 to your computer and use it in GitHub Desktop.

Revisions

  1. mainda01 created this gist May 7, 2024.
    99 changes: 99 additions & 0 deletions sync_peerings.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    #!/bin/bash

    # Function to display help menu
    display_help() {
    echo "Usage: $0 [options]"
    echo "Options:"
    echo " -s, --subscription <subscription_name> Specify the Azure subscription name"
    echo " -h, --help Display this help menu"
    }

    # Function to prompt user for confirmation
    confirm_action() {
    #while true; do
    read -p "Do you want to resync this peering? (yes/no): " yn
    case $yn in
    [Yy]* ) return 0;;
    [Nn]* ) return 1;;
    * ) echo "Please answer yes or no.";;
    esac
    #done
    }

    # Check if Azure CLI is installed
    if ! command -v az &> /dev/null; then
    echo "Azure CLI is not installed. Please install it before running this script."
    exit 1
    fi

    # Parse command line options
    while [[ $# -gt 0 ]]; do
    case "$1" in
    -s|--subscription)
    if [ -n "$2" ]; then
    subscription_name=$2
    shift
    else
    echo "Error: Subscription name not provided."
    display_help
    exit 1
    fi
    ;;
    -h|--help)
    display_help
    exit 0
    ;;
    *)
    echo "Error: Invalid option: $1"
    display_help
    exit 1
    ;;
    esac
    shift
    done

    # Check if subscription name is provided
    if [ -z "$subscription_name" ]; then
    echo "Error: Subscription name not provided."
    display_help
    exit 1
    fi

    # Log in to Azure
    # az login

    # Set the subscription
    az account set --subscription "$subscription_name"

    # Get a list of all virtual networks
    virtual_networks=$(az network vnet list --query "[].{Name:name, ResourceGroup:resourceGroup}" -o tsv)

    # Output header
    echo "Resyncing peerings for virtual networks in subscription '$subscription_name':"

    # Loop through each virtual network
    while IFS=$'\t' read -r vnet_name resource_group; do
    echo "Virtual Network: $vnet_name (Resource Group: $resource_group)"

    # Get peerings for the virtual network
    peerings=$(az network vnet peering list --vnet-name "$vnet_name" --resource-group "$resource_group" --query "[].{Name:name, PeeringState:peeringState}" -o tsv)

    # Loop through each peering
    while IFS=$'\t' read -r peering_name peering_state; do
    echo " - Peering: $peering_name (State: $peering_state)"

    # Check if peering needs resyncing
    if [[ "$peering_state" == "Disconnected" || "$peering_state" == "Initiated" ]]; then
    if confirm_action; then
    echo " - Resyncing peering: $peering_name"
    az network vnet peering update --name "$peering_name" --vnet-name "$vnet_name" --resource-group "$resource_group" --set peeringState=Connected -o none
    else
    echo " - Skipping resync for peering: $peering_name"
    fi
    else
    echo " - Peering is already connected. Skipping resync."
    fi
    done <<< "$peerings"
    done <<< "$virtual_networks"

    echo "Peerings resynced successfully."