简体   繁体   English

无法在 Azure 中使用 Python SDK 获取虚拟机规模集的公共 IP 地址

[英]Not able to fetch public IP address of virtual machine scale set with Python SDK in Azure

I have a virtual machine scale set and I have two VMs attached to it and I want to fetch its public IP address with the Python SDK.我有一个虚拟机规模集,并且有两个 VM 连接到它,我想使用 Python SDK 获取其公共 IP 地址。

I am using this script to fetch IP configurations.我正在使用此脚本来获取 IP 配置。

def get_vmss_vm_ips():

    # List all network interfaces of the VMSS instance
        vmss_nics = network_client.network_interfaces.list_virtual_machine_scale_set_network_interfaces(
            rg, vmscalesetname)

        niclist = [nic.serialize() for nic in vmss_nics]

        print "IP addresses in the given VM Scale Set:"

        for nic in niclist:
            ipconf = nic['properties']['ipConfigurations']
            print ipconf

This is my output:这是我的输出:

{'id': u'/subscriptions/sub_id/resourceGroups/test/providers/Microsoft.Compute/virtualMachineScaleSets/EchoServer/virtualMachines/2/networkInterfaces/test-vnet-nic01/ipConfigurations/test-vnet-nic01-defaultIpConfiguration', 'properties': {'subnet': {'id': u'/subscriptions/sub_id/resourceGroups/test/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/default'}, 'privateIPAddressVersion': u'IPv4', 'publicIPAddress': {'id': u'/subscriptions/sub_id/resourceGroups/test/providers/Microsoft.Compute/virtualMachineScaleSets/EchoServer/virtualMachines/2/networkInterfaces/test-vnet-nic01/ipConfigurations/test-vnet-nic01-defaultIpConfiguration/publicIPAddresses/publicIp-test-vnet-nic01'}, 'privateIPAllocationMethod': u'Dynamic', 'primary': True, 'privateIPAddress': u'10.0.0.9'}, 'name': u'test-vnet-nic01-defaultIpConfiguration'}]

I can see privateIPAddress , but not publicIPAddress .我可以看到privateIPAddress ,但不能看到publicIPAddress How do I fetch the public IP address?如何获取公共 IP 地址?

If you want to the Azure scale set virtual machines' public IP address, please refer to the following script如果要获取 Azure 规模集虚拟机的公网 IP 地址,请参考以下脚本

  1. Create a service principal and assign Contributor role to the service principal创建服务主体并将Contributor角色分配给服务主体

    az login az ad sp create-for-rbac -n "MyApp" --role contributor \\ --scopes /subscriptions/{SubID} --sdk-auth true
  2. Code代码

    from azure.mgmt.network import NetworkManagementClient from azure.identity import ClientSecretCredential client_id = "<sp appId>" secret = "<sp password>" tenant = "<sp tenant>" Subscription_ID = "<>" creds = ClientSecretCredential( client_id=client_id, client_secret=secret, tenant_id=tenant) network_client = NetworkManagementClient(creds, Subscription_ID) resource_group_name = "testVm_group" virtual_machine_scale_set_name = "testVm" results = network_client.public_ip_addresses.list_virtual_machine_scale_set_public_ip_addresses( resource_group_name, virtual_machine_scale_set_name) for result in results: print(result.id) print(result.ip_address)

    在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 虚拟机规模集代理公共 IP - Azure virtual machine scale set agents public IP Azure虚拟机公用IP地址是static吗? - Is the Azure virtual machine public IP address static? 我可以使用 Azure REST ZDB974238714CA8DE3CE38A 获取虚拟机规模集的公共 IP 吗? - Can I get the public IP of a Virtual Machine Scale Set using Azure REST API? 为现有虚拟机规模集添加公用IP - Adding public IP for existing virtual machine scale set Azure模板为每个虚拟机创建公共静态IP地址 - Azure template to create public static IP address for each virtual machine 如何从python API azure sdk获取azure scale set实例的公共IP? - how to get public IP of azure scale set instance from python API azure sdk? 使用资源管理器在虚拟机规模集上设置Azure多个公共IP - Azure Multiple Public IPs on a Virtual Machine Scale Set with Resource Manager 如何在 Azure 规模集中获取虚拟机的公共 DNS 名称 - How to get public DNS name of a Virtual Machine in Azure Scale Set 如何在没有 PUBLIC VIRTUAL IP (VIP) 地址的情况下在 Microsoft Azure 中创建虚拟机? - How to create a Virtual Machine in Microsoft Azure without PUBLIC VIRTUAL IP (VIP) address? Azure SDK中是否有python类来保留实例的公共IP地址? - Is there a python class in Azure SDK to reserve Public IP address of an instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM