在Python中使用Azure SDK从VM对象获取IP [英] Get IP from VM object using azure sdk in python

查看:100
本文介绍了在Python中使用Azure SDK从VM对象获取IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从azure订阅中获取所有IP(附加到VM).

I am trying to get all the IPs (attached to VMs) from an azure subscription.

我已使用

compute_client = ComputeManagementClient(credentials, subscription_id)
network_client = NetworkManagementClient(credentials,subscription_id)


 for vm in compute_client.virtual_machines.list_all():
      print(vm.network_profile.network_interface)

但是network_profile对象似乎只是一个指针,我已经阅读了文档,无法弄清楚如何将每个vm链接到其附加的IP地址

But the network_profile object seems to only be a pointer, I have read through the documentation and can not figure out how to link each vm to its attached IP addresses

我遇到了这个问题:但是似乎有些变化.

仅当我知道Public_IP地址对象的名称时才能够解析计算机的IP(并不是所有人都具有公共IP).

I am able to resolve the IPs of a machine only if I know the name of the Public_IP address object(Which not all of them have public IPs).

我需要能够使用该network_interface并解析其上的IP

I need to be able to take this network_interface and resolve the IP on it

推荐答案

因此,为了获得IP,您似乎需要解析vm.network_profile.network_interface中给出的URI.然后使用订阅和nic名称获取IP using network_client.network_interfaces.get().

So It seems that in order to get the IPs, you need to parse the URI given in the vm.network_profile.network_interface. Then use the the subscription and the nic name to get the IP using network_client.network_interfaces.get().

我使用的代码如下:

compute_client = ComputeManagementClient(credentials, subscription_id)
network_client = NetworkManagementClient(credentials,subscription_id)
        try:
            get_private(compute_client, network_client)
        except:
            print("Auth failed on "+ subscription_id)



def get_private(compute_client, network_client):


    for vm in compute_client.virtual_machines.list_all():
        for interface in vm.network_profile.network_interfaces:
            name=" ".join(interface.id.split('/')[-1:])
            sub="".join(interface.id.split('/')[4])

            try:
                thing=network_client.network_interfaces.get(sub, name).ip_configurations

                for x in thing:
                    print(x.private_ip_address)

            except:
                print("nope")

在此示例中,您还可以执行x.public_ip_address获取公共IP

In this example you could also do x.public_ip_address to get the public IPs

这篇关于在Python中使用Azure SDK从VM对象获取IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆