I'm trying to find public IP address attached to running Azure VM.
I've tried both answers as per Get IP from VM object using azure sdk in python but I'm not getting required output. Getting Public IP: None
...: for interface in vm.network_profile.network_interfaces:
...: name=" ".join(interface.id.split('/')[-1:])
...: print (name)
...: sub="".join(interface.id.split('/')[4])
...: print (sub)
...: thing = network_client.network_interfaces.get(sub, name).ip_configurations
...: for x in thing:
...: print (x.public_ip_address)
...:
xxx
xxx
{'additional_properties': {}, 'id': '/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/publicIPAddresses/Test-ip', 'name': None, 'type': None, 'location': None, 'tags': None, 'sku': None, 'public_ip_allocation_method': None, 'public_ip_address_version': None, 'ip_configuration': None, 'dns_settings': None, 'ddos_settings': None, 'ip_tags': None, 'ip_address': None, 'public_ip_prefix': None, 'idle_timeout_in_minutes': None, 'resource_guid': None, 'provisioning_state': None, 'etag': None, 'zones': None}
whereas using network_client I'm getting output
In [6]: from azure.mgmt.network import NetworkManagementClient
In [21]: for i in network_client.public_ip_addresses.list("xxx"):
...: print (i)
But here I'm getting output for all subscriptions, all resource group which basically I want to filter. Hence I think using custom_headers we may filter, but I'm not getting what would be the exact naming convention of dict I'd create for custom_headers.
Short version: Your question has been addressed on Github already: https://github.com/Azure/azure-sdk-for-python/issues/897
A few comments though:
custom_headers
is to define HTTP headers, which won't help for your scenario:NetworkManagementClient
you already filter by subscription, since subscription is a parameter of it.public_ip_addresses.list
is a resource group name, so you already filter by resource group name: See https://learn.microsoft.com/en-us/python/api/azure-mgmt-network/azure.mgmt.network.v2019_04_01.operations.publicipaddressesoperations?view=azure-python#list-resource-group-name--custom-headers-none--raw-false----operation-config-public_ip_addresses.get
. NIC doesn't expand to the actual Public IP value for computational reason.(I work at MS in the Python SDK team)