如何使用Azure-SDK for Python获取连接到Azure中特定虚拟机的VNET(VirtualNetwork)信息

问题描述 投票:0回答:1

我想使用Azure-SDK for Python从Azure获取有关虚拟机的信息。我可以通过在computeClient中提供资源组和虚拟机名称来获取VM的信息]

compute_client = ComputeManagementClient(
    credentials,
    SUBSCRIPTION_ID
)

compute_client.virtual_machines.get(GROUP_NAME,VM_NAME,expand ='instanceView')] >>

但是上面的代码没有给我Vnet信息

有人可以指导我吗?>

我想使用Azure-SDK for Python从Azure获取有关虚拟机的信息。我可以通过在computeClient中提供资源组和虚拟机名称来获取VM的信息...

根据您的要求,您可以从ComputeManagementClient SDK获得的只是VM的网络接口,没有诸如Vnet,子网之类的网络接口。这是网络接口的配置。

因此,您需要获取网络接口的信息,然后它将向您显示Nic的配置,其中包含Nic所在的子网。

我假设您只知道VM信息,那么您可以像这样获得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只有一个配置。如果VM具有多个Nic,并且每个Nic具有多个配置。您可以在每个循环的a内一一获取。最终,您获得了所需的有关Vnet的信息。

azure-virtual-machine azure-virtual-network azure-sdk-python
1个回答
0
投票

根据您的要求,您可以从ComputeManagementClient SDK获得的只是VM的网络接口,没有诸如Vnet,子网之类的网络接口。这是网络接口的配置。

© www.soinside.com 2019 - 2024. All rights reserved.