有没有一种方法可以自动以编程方式下载Microsoft Azure使用的最新IP范围? [英] Is there a way to automatically and programmatically download the latest IP ranges used by Microsoft Azure?
问题描述
Microsoft提供了一个URL,您可以在其中下载Microsoft Azure使用的公共IP范围.
Microsoft has provided a URL where you can download the public IP ranges used by Microsoft Azure.
https://www.microsoft.com/zh-CN-us/download/confirmation.aspx?id = 41653
问题是,有没有办法使用Python或其他脚本从该站点自动下载XML文件?我正在尝试安排一项任务,以获取新的IP范围文件并为我的防火墙策略处理它.
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.
推荐答案
是!有!
但是,您当然需要做比预期更多的工作.
当我意识到Microsoft页仅在加载到浏览器中时才可以通过JavaScript运行时,才发现了这个问题.这使得通过Curl实现的自动化几乎无法使用;更新发生时,我不是手动下载此文件.取而代之的是,IO提出了这个对我来说效果很好的Bash单线".
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"
现在只需运行此Curl命令(该命令将使用几个链接的Greps解析返回的网页HTML)即可,如下所示:
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)://[^"]+'
返回的输出是一个非常干净的最终URL,如下所示:
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
完全是一个人,需要它来自动直接下载该XML文件.然后,如果您需要将该值分配给URL,只需将其包装在 $()
中,如下所示:
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)://[^"]+')
然后只需通过 $ MICROSOFT_IP_RANGES_URL_FINAL
来访问该URL,即可.
And just access that URL via $MICROSOFT_IP_RANGES_URL_FINAL
and there you go.
这篇关于有没有一种方法可以自动以编程方式下载Microsoft Azure使用的最新IP范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!