查找Azure VM的Infiniband IP地址

时间:2019-11-16 00:52:21

标签: python azure hpc azure-sdk-python

这类似于Is there any python API which can get the IP address (internal or external) of Virtual machine in Azure,但我想获取一个Azure VM的infiniband IP地址。

到目前为止,我可以在VMSS中获得VM的NIC,但这只能让我查询eth0的专用IP地址。如何获取其他接口的IP地址?

理想情况下,我想在Python中做到这一点


from azure.mgmt.network import NetworkManagementClient
self.network_client = NetworkManagementClient(credentials, AZURE_SUBSCRIPTION_ID)

# get the private IP of a network interface
nic_name = 'redacted'
network_client.network_interfaces.get(GROUP_NAME, nic_name)
private_ip = nic.ip_configurations[0].private_ip_address

1 个答案:

答案 0 :(得分:0)

如果要求获取VMSS实例中VM /网络接口的所有IP,则可以使用官方的Azure SDK for Python,如下所示:

# Imports
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient

# Set subscription ID
SUBSCRIPTION_ID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'


def get_credentials():
    credentials = ServicePrincipalCredentials(
        client_id='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        secret='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
        tenant='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    )

    return credentials


# Get credentials
credentials = get_credentials()


# Initialize management client
network_client = NetworkManagementClient(
    credentials,
    SUBSCRIPTION_ID
)


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(
        "<VMSS Resource group name>", "<VMSS instance name>")

    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']

        for ip in ipconf:
            print ip['properties']['privateIPAddress']


# Get all IPs of VMs in VMSS
get_vmss_vm_ips()

请注意,network_client.network_interfaces.get()方法仅获取有关指定网络接口的信息,而list_virtual_machine_scale_set_network_interfaces()方法则获取虚拟机规模集中的所有网络接口。

参考:

希望这会有所帮助!否则请通知我,我们可以进一步探索。