我想使用Azure-SDK for Python从Azure获取有关虚拟机的信息。我可以通过在computeClient中提供资源组和虚拟机名称来获取虚拟机的信息
compute_client = ComputeManagementClient(
credentials,
SUBSCRIPTION_ID
)compute_client.virtual_machines.get(GROUP_NAME,VM_NAME,展开=‘instanceView’)
但是上面的代码没有给我Vnet信息
有人能在这方面给我指点一下吗
发布于 2020-03-16 10:46:22
根据您的需求,您可以从ComputeManagementClient软件开发工具包中获得的只是虚拟机的网络接口,而不是Vnet、子网。这是网络接口的配置。
因此,您需要做的是获取网络接口的信息,然后它将显示Nic的配置,其中包含Nic所在的子网。
我假设你只知道虚拟机信息,那么你可以像这样获得Vnet信息:
from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient
subscription_Id = "xxxxxxxxx"
tenant_Id = "xxxxxxxxx"
client_Id = "xxxxxxxxx"
secret = "xxxxxxxxx"
credential = ServicePrincipalCredentials(
client_id=client_Id,
secret=secret,
tenant=tenant_Id
)
compute_client = ComputeManagementClient(credential, subscription_Id)
group_name = 'xxxxxxxxx'
vm_name = 'xxxxxxxxx'
vm = compute_client.virtual_machines.get(group_name, vm_name)
nic_name = vm.network_profile.network_interfaces[0].id.split('/')[-1]
nic_group = vm.network_profile.network_interfaces[0].id.split('/')[-5]
network_client = NetworkManagementClient(credential, subscription_Id)
nic = network_client.network_interfaces.get(nic_group, nic_name)
vnet_name = nic.ip_configurations[0].subnet.id.split('/')[-3]
vnet_group = nic.ip_configurations[0].subnet.id.split('/')[-7]
vnet = network_client.virtual_networks.get(vnet_group, vnet_name)以上所有代码,我假设虚拟机只有一个Nic,并且Nic只有一个配置。如果虚拟机有多个Nic,并且每个Nic都有多个配置。您可以在for each循环中逐个获取它们。最后,你得到了你想要的Vnet的信息。
https://stackoverflow.com/questions/60668147
复制相似问题