简体   繁体   English

是否有任何python API可以获取Azure中虚拟机的IP地址(内部或外部)

[英]Is there any python API which can get the IP address (internal or external) of Virtual machine in Azure

I want to control the VMs in Azure with python SDK. 我想用python SDK控制Azure中的VM。 Is there any API that can get a VM's IP address (internal or external) according to VM's name? 是否有任何API可以根据VM的名称获取VM的IP地址(内部或外部)?

Based on my understanding, I think you want to get the public & private ip addresses of a Azure VM using Azure SDK for Python. 根据我的理解,我认为您希望使用Azure SDK for Python获取Azure VM的公共和私有IP地址。

For getting these ip addresses (internal & external), please see the code below. 要获取这些IP地址(内部和外部),请参阅下面的代码。

from azure.mgmt.network import NetworkManagementClient, NetworkManagementClientConfiguration

subscription_id = '33333333-3333-3333-3333-333333333333'

credentials = ...

network_client = NetworkManagementClient(
    NetworkManagementClientConfiguration(
        credentials,
        subscription_id
    )
)

GROUP_NAME = 'XXX'
VM_NAME = 'xxx'
PUBLIC_IP_NAME = VM_NAME

public_ip_address = network_client.public_ip_addresses.get(GROUP_NAME, PUBLIC_IP_NAME)
print(public_ip_address.ip_address)
print(public_ip_address.ip_configuration.private_ip_address)

As reference, you can refer to the documents below to know the details of the code above. 作为参考,您可以参考以下文档以了解上述代码的详细信息。

  1. Resource Management Authentication using Python for the variable credentials . 使用Python进行可变credentials 资源管理身份验证
  2. Create the management client for the variable network_client . 为变量network_client 创建管理客户端
  3. More details for the azure.mgmt.network package, please see http://azure-sdk-for-python.readthedocs.io/en/latest/ref/azure.mgmt.network.html . 有关azure.mgmt.network包的更多详细信息,请参阅http://azure-sdk-for-python.readthedocs.io/en/latest/ref/azure.mgmt.network.html

For the people that don't have public IP assigned to their VM there is a way to get the private IP without creating the public IP. 对于没有为其VM分配公共IP的人,有一种方法可以在不创建公共IP的情况下获取私有IP。

from azure.mgmt.network import NetworkManagementClient, NetworkManagementClientConfiguration

subscription_id = '' 
credentials = UserPassCredentials(
    'user@yes.com',
    'password',
)

network_client = NetworkManagementClient(
    credentials,
    subscription_id
)

private_ip = network_client.network_interfaces.get(GROUP_NAME, NETWORK_INTERFACE_NAME).ip_configurations[0].private_ip_address

This works and is tested on azure-sdk-for-python version 2.0.0rc6. 这可以在azure-sdk-for-python 2.0.0rc6版本上进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM