首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Azure-SDK for Python获取连接到Azure中特定虚拟机的VNET (VirtualNetwork)信息

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

Stack Overflow用户
提问于 2020-03-13 17:45:58
回答 1查看 684关注 0票数 0

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

代码语言:javascript
复制
compute_client = ComputeManagementClient(
    credentials,
    SUBSCRIPTION_ID
)

compute_client.virtual_machines.get(GROUP_NAME,VM_NAME,展开=‘instanceView’)

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

有人能在这方面给我指点一下吗

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-16 10:46:22

根据您的需求,您可以从ComputeManagementClient软件开发工具包中获得的只是虚拟机的网络接口,而不是Vnet、子网。这是网络接口的配置。

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

我假设你只知道虚拟机信息,那么你可以像这样获得Vnet信息:

代码语言:javascript
复制
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的信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60668147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档