英文:
How to check if Azure public IP is 'associated' via Python SDK
问题
我正在尝试获取未关联到任何 Azure 资源的公共 IP 地址列表。也就是说,'孤立的公共 IP 地址'。我想知道如何通过 Python SDK 判断 Azure 公共 IP 是否已经 '关联'。
使用以下 SDK:
from azure.mgmt.network import NetworkManagementClient
network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID)
public_ip_list = network_client.public_ip_addresses.list_all()
遍历 'public_ip_list' 将提供所有有关 IP 的详细信息,但不会告诉我它是否与任何 Azure 资源关联。
英文:
I am trying to get a list of Public IP addresses that are not associated to any azure resource. That is 'orphaned public ip addresses'. I want to know if Azure public IP is 'associated' via Python SDK.
Using the below SDK:
from azure.mgmt.network import NetworkManagementClient
network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID)
public_ip_list = network_client.public_ip_addresses.list_all()
Iterating over 'public_ip_list' will give me all the details regarding IP, but it wont say if it is 'associated' with any azure resource or not.
答案1
得分: 1
你可以使用Azure Python SDK来获取与Azure服务关联和未关联的公共IP地址列表。
当你将ip_config设置为none时,可以获取未关联Azure资源的公共IP,同时你还可以获取关联和未关联IP的计数。
代码:
from azure.mgmt.network import NetworkManagementClient
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
Subscription_id="your-subscription-id"
network_client = NetworkManagementClient(credential, Subscription_id)
public_ip_list = network_client.public_ip_addresses.list_all()
associated_count = 0
non_associated_count = 0
for public_ip in public_ip_list:
if public_ip.ip_configuration is None:
non_associated_count += 1
print(f"Public IP address {public_ip.name} is not associated with any Azure resource.")
else:
associated_count += 1
print(f"Public IP address {public_ip.name} is associated with Azure resource {public_ip.ip_configuration.id}.")
print("Count of Non-associated with resource:", non_associated_count)
print("Count of associated with resource:", associated_count)
示例输出:
Public IP address xxxxxxx is associated with Azure resource /subscriptions/xxxxx/resourceGroups/xxx/providers/Microsoft.Network/networkInterfaces/xxxx/ipConfigurations/primary.
Public IP address xx is not associated with any Azure resource.
Count of Non-associated with resource: 26
Count of associated with resource: 79
参考链接:
Public IP Addresses - List All - REST API (Azure Virtual Networks) | Microsoft Learn
英文:
> I am trying to get a list of Public IP addresses that are not associated with any Azure resource
You can use the below to get both associated and non-associated Public IPs with Azure service using Azure python sdk.
You can get the public IP that is not associated with Azure resource when you set ip_config to none and also you can get the count of both associated and non-associated IP.
Code:
from azure.mgmt.network import NetworkManagementClient
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
Subscription_id="your-subscription-id"
network_client = NetworkManagementClient(credential,Subscription_id)
public_ip_list = network_client.public_ip_addresses.list_all()
associated_count = 0
non_associated_count=0
for public_ip in public_ip_list:
if public_ip.ip_configuration is None:
non_associated_count+=1
print(f"Public IP address {public_ip.name} is not associated with any Azure resource.")
else:
associated_count += 1
print(f"Public IP address {public_ip.name} is associated with Azure resource {public_ip.ip_configuration.id}.")
print("Count of Non-associated with resource:",non_associated_count)
print("Count of associated with resource:",associated_count)
Sample Output:
Public IP address xxxxxxx is associated with Azure resource /subscriptions/xxxxx/resourceGroups/xxx/providers/Microsoft.Network/networkInterfaces/xxxx/ipConfigurations/primary.
Public IP address xx is not associated with any Azure resource.
Count of Non-associated with resource: 26
Count of associated with resource: 79
Reference:
Public IP Addresses - List All - REST API (Azure Virtual Networks) | Microsoft Learn
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论