Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to automatically and programmatically download the latest IP ranges used by Microsoft Azure?

Tags:

azure

Microsoft has provided a URL where you can download the public IP ranges used by Microsoft Azure.

https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653

The question is, is there a way to download the XML file from that site automatically using Python or other scripts? I am trying to schedule a task to grab the new IP range file and process it for my firewall policies.

like image 808
nicklee Avatar asked Mar 01 '15 19:03

nicklee


People also ask

What are the 5 reserved IP in Azure?

Per Microsoft: "Azure reserves 5 IP addresses within each subnet. These are x.x.x.0-x.x.x.3 and the last address of the subnet. x.x.x.1-x.x.x.3 is reserved in each subnet for Azure services.

How do I find the IP address of my Azure app?

To find the outbound IP addresses currently used by your app in the Azure portal, click Properties in your app's left-hand navigation. They are listed in the Outbound IP Addresses field.


1 Answers

Yes! There is!

But of course you need to do a bit more work than expected to achieve this.

I discovered this question when I realized Microsoft’s page only works via JavaScript when loaded in a browser. Which makes automation via Curl practically unusable; I’m not manually downloading this file when updates happen. Instead IO came up with this Bash “one-liner” that works well for me.

First, let’s define that base URL like this:

MICROSOFT_IP_RANGES_URL="https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653"

Now just run this Curl command—that parses the returned web page HTML with a few chained Greps—like this:

curl -Lfs "${MICROSOFT_IP_RANGES_URL}" | grep -Eoi '<a [^>]+>' | grep -Eo 'href="[^\"]+"' | grep "download.microsoft.com/download/" | grep -m 1 -Eo '(http|https)://[^"]+'

The returned output is a nice and clean final URL like this:

https://download.microsoft.com/download/0/1/8/018E208D-54F8-44CD-AA26-CD7BC9524A8C/PublicIPs_20181224.xml

Which is exactly what one needs to automatic the direct download of that XML file. If you need to then assign that value to a URL just wrap it in $() like this:

MICROSOFT_IP_RANGES_URL_FINAL=$(curl -Lfs "${MICROSOFT_IP_RANGES_URL}" | grep -Eoi '<a [^>]+>' | grep -Eo 'href="[^\"]+"' | grep "download.microsoft.com/download/" | grep -m 1 -Eo '(http|https)://[^"]+')

And just access that URL via $MICROSOFT_IP_RANGES_URL_FINAL and there you go.

like image 54
Giacomo1968 Avatar answered Sep 26 '22 13:09

Giacomo1968