agent: |
CW0JA7eBzXsSkAwm9EXFList All Public IP Addresses in Azure
List All Public IP Addresses in Azure
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task retrieves and displays all public IP addresses provisioned within an Azure subscription, regardless of their association with resources.
inputs
outputs
import azure.identity
from azure.mgmt.network import NetworkManagementClient
# Initialize Azure credentials
credential = azure.identity.DefaultAzureCredential()
# Optional location parameter. Set to None for all regions, or specify a region like "eastus".
#location = 'eastus' # or "eastus", "westus", etc.
# Setup Azure Network client
network_client = NetworkManagementClient(credential, subscription_id)
try:
# List all public IP addresses
all_ips = list(network_client.public_ip_addresses.list_all())
# Filter by location if specified
if location:
all_ips = [ip for ip in all_ips if ip.location == location]
if all_ips:
print(f"Public IP Addresses in {'all regions' if not location else location}:")
print("-" * 60)
print(f"{'Name':<20} | {'IP Address':<20} | {'Location':<10}")
print("-" * 60)
for ip in all_ips:
print(f"{ip.name:<20} | {ip.ip_address:<20} | {ip.location:<10}")
print("-" * 60)
else:
print(f"No Public IP addresses found in {'your Azure subscription' if not location else location}.")
except azure.core.exceptions.HttpResponseError as hr_err:
print(f"Azure SDK Error: {hr_err.message}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
copied