[英]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 地址,请参考以下脚本
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
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.