Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save florian-obradovic/43c451e336fd4f2bf6f2c342fec64e4a to your computer and use it in GitHub Desktop.
Save florian-obradovic/43c451e336fd4f2bf6f2c342fec64e4a to your computer and use it in GitHub Desktop.
Get Azure IPs from a JSON file, filter for specific fields and get the IP addressPrefixes for a specific service tag. Create a MikroTik RouterOS CLI command to add the IP to an address list!
# Use Case:
# Get IPs from a JSON file, filter for specific fields and get the IP addressPrefixes for a specific service tag.
# Create a MikroTik RouterOS CLI command to add the IP to an address list!
# Azure IP Ranges and Service Tags: https://www.microsoft.com/en-us/download/details.aspx?id=56519
# Load the JSON file
$jsonData = Get-Content -Path "/Users/flo/Downloads/ServiceTags_Public_20241007.json" -Raw | ConvertFrom-Json
# Define the output file path
$outputFilePath = "/Users/flo/Downloads/Mikrotik.rsc"
# Filter the data to get addressPrefixes for a systemService and a region contains "germany" or "europe"
$addressPrefixes = $jsonData.values | Where-Object {
$_.properties.systemService -eq "AzureServiceBus"
# -and ($_.properties.region -contains "germany" -or $_.properties.region -match "europe")
} | Select-Object -ExpandProperty properties | Select-Object -ExpandProperty addressPrefixes
# Output the addressPrefixes
$addressPrefixes
# Initialize the CSV content
$csvContent = @()
# Loop through the addressPrefixes and generate the commands
foreach ($address in $addressPrefixes) {
if ($address -match ":") {
$command = "/ipv6/firewall/address-list/add address=""$address"" list=""Microsoft-Azure-Connectors"" comment=""Microsoft-Azure-Connectors TI-9964"""
} else {
$command = "/ip/firewall/address-list/add address=""$address"" list=""Microsoft-Azure-Connectors"" comment=""Microsoft-Azure-Connectors TI-9964"""
}
# Output the command to the console
Write-Host $command
# Add the command to the CSV content
$csvContent += $command
}
# Write the CSV content to the file
$csvContent | Out-File -FilePath $outputFilePath -Encoding utf8
# Add the command to the CSV content
$csvContent += $command
@florian-obradovic
Copy link
Author

Updated the script to output only the commands...

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