How to get public ip address for virtual-machine using Azure CLI command
Asked Answered
F

4

20

I want to get public IP address for a specific virtual machine in Azure bash command line, I have used this command so far but it returns network interface information:

az vm list-ip-addresses -g dev-rg -n dev-vm

returned value:

[
  {
    "virtualMachine": {
      "name": "dev-vm",
      "network": {
        "privateIpAddresses": [
          "10.0.0.5"
        ],
        "publicIpAddresses": [
          {
            "id": "/subscriptions/*********/resourceGroups/dev-rg/providers/Microsoft.Network/publicIPAddresses/dev-vmPublicIP",
            "ipAddress": "52.142.***.***",
            "ipAllocationMethod": "Dynamic",
            "name": "dev-vmPublicIP",
            "resourceGroup": "dev-rg"
          }
        ]
      },
      "resourceGroup": "dev-rg"
    }
  }
]

I only need the IP address value which should be something like this: 52.142.xxx.xxx

Fallonfallout answered 4/3, 2019 at 9:31 Comment(0)
D
34

You can just use the CLI command az vm show -d -g resourceGroupName -n vmName --query publicIps -o tsv to output the public IP.

It just shows like this:

enter image description here

Dialectal answered 4/3, 2019 at 9:34 Comment(0)
C
7

You could also continue with your initial idea (key points are the --query and --output parameters):

az vm list-ip-addresses --resource-group dev-rg --name dev-vm --query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" --output tsv

As the documentation states, using az vm show --show-details could be slow.

Choirmaster answered 6/4, 2021 at 23:26 Comment(0)
D
4

If you want to get public IPs for many machine you can also use the az network public-ip:

az network public-ip list -o table
Doubleedged answered 14/12, 2021 at 8:20 Comment(0)
L
2

Short Command

az vm list-ip-addresses -n SampleVM -o table

Reference:

https://learn.microsoft.com/en-us/learn/modules/manage-virtual-machines-with-azure-cli/6-querying-vms

Lynnalynne answered 25/11, 2021 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.