如何通过Python SDK检查Azure公共IP是否“关联”

lyfkaqu1  于 2023-08-07  发布在  Python
关注(0)|答案(1)|浏览(94)

我正在尝试获取未与任何Azure资源关联的公共IP地址列表。这就是“孤立的公共IP地址”。我想知道Azure公共IP是否通过Python SDK“关联”。
使用下面的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资源“关联”。

n53p2ov0

n53p2ov01#

我正在尝试获取未与任何Azure资源关联的公共IP地址列表
您可以使用下面的命令来使用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


x1c 0d1x的数据

参考号:

公有IP地址-全部列出- REST API(Azure虚拟网络)|Microsoft Learn

相关问题