有没有办法用Java下载Microsoft Azure数据中心IP范围?

时间:2016-09-08 21:29:27

标签: java azure

我的问题与Is there a way to automatically and programmatically download the latest IP ranges used by Microsoft Azure?类似,只是我尝试使用Java来执行此操作。

我正在使用Javaq从https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653抓取下载链接,然后从URL再次下载,但我认为MS Azure应提供一些API或更简单的选项来执行此操作。每个人都需要这个用于防火墙中的白名单。

任何有更好的Java选项的人都请告诉我。

2 个答案:

答案 0 :(得分:0)

在完成downloadurl之后,已经整理了一个粗略的代码来提取IP地址。如果Azure将来在下载URL中更改某些内容,则可能无法正常工作。

private static final String baseURI = "https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653";
private static final String downloadURI_Part = "https://download.microsoft.com";
private static final String HREF = "href";

public static void main(String[] args) {
    new DownloadXML().parse();
}

public void parse() {
    try {
        URL url = new URL(baseURI);
        String downloadURL = "";
        org.jsoup.nodes.Document doc1 = Jsoup.connect(url.toString()).get();
        Elements newsHeadlines = doc1.select("a");
        for (org.jsoup.nodes.Element element : newsHeadlines) {
            if (element.hasAttr(HREF) && element.getElementsByAttribute(HREF).attr(HREF)
                    .contains(downloadURI_Part)) {
                downloadURL = element.getElementsByAttribute(HREF).attr(HREF);
                System.out.println(element.getElementsByAttribute(HREF).attr(HREF));
            }
        }
        System.out.println(downloadURL);
        URL url1 = new URL(downloadURL);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(url1.openStream());
        doc.getDocumentElement().normalize();
        NodeList nRegionList = doc.getElementsByTagName("Region");
        System.out.println("----------------------------");

        for (int nRegionCount = 0; nRegionCount < nRegionList.getLength(); nRegionCount++) {
            Node nRegionNode = nRegionList.item(nRegionCount);
            System.out.println("\nCurrent Element :" + nRegionNode.getNodeName());
            if (nRegionNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nRegionNode;
                System.out.println("Region name: " + eElement.getAttribute("Name"));
                NodeList nIPRangeList = eElement.getChildNodes();
                for (int iprangecnt = 0; iprangecnt < nIPRangeList.getLength(); iprangecnt++) {
                    Node nIPRNode = nIPRangeList.item(iprangecnt);
                    if (nIPRNode.hasAttributes()) {
                        // get attributes names and values
                        NamedNodeMap nodeMap = nIPRNode.getAttributes();
                        for (int i = 0; i < nodeMap.getLength(); i++) {
                            Node node = nodeMap.item(i);
                            System.out.println("attr name : " + node.getNodeName());
                            System.out.println("attr value : " + node.getNodeValue());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

这在 PowerShell v5+ 中对我有用:

$rawhtml = Invoke-RestMethod -Uri 'https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653' -UseBasicParsing
[Regex]::Match($rawhtml, 'https://download.microsoft.com[^"]*').Value