Azure-sdk-for-python: Getting IP address of virtual machines

Created on 25 Nov 2016  路  5Comments  路  Source: Azure/azure-sdk-for-python

I'm trying to get a list of virtual machines and their IP addresses (so as to be able to SSH to them, etc.), as per:

azure_subscription = os.environ['AZURE_SUBSCRIPTION']
azure_email = os.environ['AZURE_EMAIL']
azure_password = os.environ['AZURE_PASSWORD']

# Connect to account
credentials = UserPassCredentials(
    azure_email,  # Your user
    azure_password,  # Your password
)
compute_client = ComputeManagementClient(
    credentials,
    azure_subscription,
)

# List istances
instance_list = compute_client.virtual_machines.list_all()
for i, instance in enumerate(instance_list):
    print ('{}) {} - {}').format(i, t.red(instance.name), instance.hardware_profile.vm_size)

However, on inspecting the virtual_machine objects found, I can't find a public_ip property in the fields or sub-fields.

How can I extract the public IP of each of the machines?

Compute Network Network - Virtual Network Service Attention question

Most helpful comment

Ended up doing something like this:

network_client = NetworkManagementClient(
    credentials,
    azure_subscription,
)

# Unfortunate and convoluted way of obtaining public IP of selected instance
ni_reference = instance.network_profile.network_interfaces[0]
ni_reference = ni_reference.id.split('/')
ni_group = ni_reference[4]
ni_name = ni_reference[8]

net_interface = network_client.network_interfaces.get(ni_group, ni_name)
ip_reference = net_interface.ip_configurations[0].public_ip_address
ip_reference = ip_reference.id.split('/')
ip_group = ip_reference[4]
ip_name = ip_reference[8]

public_ip = network_client.public_ip_addresses.get(ip_group, ip_name)
public_ip = public_ip.ip_address

Surely, there must be a much simpler way of getting the IP address than this, but what is it?

All 5 comments

Ended up doing something like this:

network_client = NetworkManagementClient(
    credentials,
    azure_subscription,
)

# Unfortunate and convoluted way of obtaining public IP of selected instance
ni_reference = instance.network_profile.network_interfaces[0]
ni_reference = ni_reference.id.split('/')
ni_group = ni_reference[4]
ni_name = ni_reference[8]

net_interface = network_client.network_interfaces.get(ni_group, ni_name)
ip_reference = net_interface.ip_configurations[0].public_ip_address
ip_reference = ip_reference.id.split('/')
ip_group = ip_reference[4]
ip_name = ip_reference[8]

public_ip = network_client.public_ip_addresses.get(ip_group, ip_name)
public_ip = public_ip.ip_address

Surely, there must be a much simpler way of getting the IP address than this, but what is it?

Hi @alexvicegrab

Unfortunately, you're right, this is the only way to get the actual PublicIP, since the VM is hosted by the Compute provider and the IPs by the Network provider. This is a limitation of the RestAPI:

Since this is an auto-generated SDK, this is difficult to generate less than 3 Python calls from 3 RestAPI calls. However, I agree that this might be interesting to provide:

  • An ID parser, so you avoid the split part (useful for all packages, in azure-common)
  • A PublicIP / NIC getter as a tool provided in the azure-mgmt-compute package

An ID parser, so you avoid the split part (useful for all packages, in azure-common)

This actually exists these days as msrestazure.tools.parse_resource_id: https://github.com/Azure/msrestazure-for-python/blob/594ee8a8412b176b62747aa0f89132242483ae5d/msrestazure/tools.py#L104

Thanks @akx ! Indeed, since I wrote this we added the ID parse to msrestazure :)

Update: Nowadays, if provisioning a VM, I would use Terraform instead, rather than the python API. This will allow me to get the Public IP via one of the attached resources on the Terraform output.tf.

Closing this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fajkowsky picture Fajkowsky  路  4Comments

jmlero picture jmlero  路  3Comments

shreyasmoolya09 picture shreyasmoolya09  路  3Comments

lumigogogo picture lumigogogo  路  3Comments

stawo picture stawo  路  3Comments