【问题标题】:how to get public IP of azure scale set instance from python API azure sdk?如何从 python API azure sdk 获取 Azure 规模集实例的公共 IP?
【发布时间】:2019-07-07 13:08:04
【问题描述】:

我已经为每个实例分配了公共 IP(没有负载均衡器),我试图从 python 代码中获取它的公共 IP 但没有运气,我到目前为止尝试了什么:

from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient      
credentials = ServicePrincipalCredentials(client_id=ID, secret=SECRET_KEY, tenant=TENANT_ID)
for net in NetworkManagementClient(credentials, SUBSCRIPTION_ID):
    print net

IP 不在此处。 我也尝试过从此返回的规模集对象:

vmss =  ComputeManagementClient(credentials, SUBSCRIPTION_ID).virtual_machine_scale_set_vms.list(resource_group_name=resource_group,
                                                                           virtual_machine_scale_set_name=scale_set_name)

但我没有在其中看到公共 IP 的属性。

【问题讨论】:

    标签: python azure azure-devops azure-functions azure-sdk


    【解决方案1】:

    我自己也不确定,所以我看了一下。原来在lists all public IP addresses of a scale set的虚拟网络服务下有一个API。

    此代码应该适合您,它将列出规模集中使用的所有公共 IP 地址。

    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.network import NetworkManagementClient
    
    # Your Azure Subscription ID
    subscription_id = 'xxxx-xxxx-xxxx'
    
    compute_client = ComputeManagementClient(credentials, subscription_id)
    network_client = NetworkManagementClient(credentials, subscription_id)
    
    rg = 'testscaleset-rg'
    scaleset_name = 'testscaleset'
    
    for i, vm in enumerate(compute_client.virtual_machine_scale_set_vms.list(resource_group_name=rg, virtual_machine_scale_set_name=scaleset_name)):
        nic_name = (vm.network_profile.network_interfaces[0].id).split("/")[-1]
        ip_config_name = vm.network_profile_configuration \
                   .network_interface_configurations[0]\
                   .ip_configurations[0]\
                   .name
        ip_address_name = vm.network_profile_configuration \
               .network_interface_configurations[0]\
               .ip_configurations[0]\
               .public_ip_address_configuration\
               .name
        print(vm.name, (network_client.public_ip_addresses.get_virtual_machine_scale_set_public_ip_address( \
            resource_group_name=rg, \
            virtual_machine_scale_set_name=scaleset_name,\
            virtualmachine_index=i, \
            network_interface_name=nic_name, \
            ip_configuration_name=ip_config_name, \
            public_ip_address_name=ip_address_name)).ip_address)
    

    应该返回

    testscaleset_0 40.68.133.234
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 2020-05-25
      相关资源
      最近更新 更多