Azure Network Management Client Library

30.2.0 · active · verified Thu Apr 09

The Microsoft Azure Network Management Client Library for Python provides programmatic access to manage network resources within Azure subscriptions, including virtual networks, public IPs, network security groups, and more. It is part of the broader Azure SDK for Python and is currently at version 30.2.0, with regular updates aligning with Azure service API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and list all public IP addresses across your subscription using the `NetworkManagementClient`. Ensure you have `AZURE_SUBSCRIPTION_ID` and other necessary authentication environment variables set for `DefaultAzureCredential` to work correctly (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET` for service principal, or log in via Azure CLI).

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.network import NetworkManagementClient

# Retrieve subscription ID from environment variable or replace with your ID
# You must set AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
# (or use other authentication methods supported by DefaultAzureCredential)
SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")

if SUBSCRIPTION_ID == "YOUR_SUBSCRIPTION_ID":
    print("Warning: Please set the AZURE_SUBSCRIPTION_ID environment variable.")
    print("Or replace 'YOUR_SUBSCRIPTION_ID' in the code with your actual ID.")
    exit(1)

# Authenticate using DefaultAzureCredential (recommended for most scenarios)
try:
    credential = DefaultAzureCredential()
    # For local development, ensure Azure CLI is logged in, or relevant env vars are set
    credential.get_token("https://management.azure.com/.default") # Test token acquisition
except Exception as e:
    print(f"Authentication failed: {e}")
    print("Please ensure you are logged into Azure CLI, have appropriate environment variables set,")
    print("or use a different credential type.")
    exit(1)

# Create a NetworkManagementClient instance
network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID)

# Example: List all Public IP Addresses in the subscription
print("Listing Public IP Addresses...")
public_ips = network_client.public_ip_addresses.list_all()

for ip in public_ips:
    print(f"  - Name: {ip.name}, IP Address: {ip.ip_address or 'N/A'}, Resource Group: {ip.id.split('/')[4]}")

print("Public IP Address listing complete.")

view raw JSON →