I want to get the ip of the virtual machine.

hmkwon@lgcns.com 21 Reputation points
2021-02-08T08:08:36.693+00:00

https://learn.microsoft.com/en-us/azure/virtual-machines/windows/python#create-a-visual-studio-project
Hello I am fetching VM information with the above information. However, there is no information about ip (public, private).

What could be the way to bring it?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,279 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Monalla-MSFT 13,046 Reputation points
    2021-02-10T14:40:47.047+00:00

    Hello, Thanks for reaching out.

    It seems that in order to get the IPs using Python, 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().

    The code I used is below:

    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")
    

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

    Hope this helps.

    If the above response helped, Please "Accept as answer" so it can beneficial to the community.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.