18

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

Ehsan Zargar Ershadi
  • 24,115
  • 17
  • 65
  • 95

4 Answers4

30

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
6

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.

flags
  • 461
  • 5
  • 11
3

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
Dan Ciborowski - MSFT
  • 6,807
  • 10
  • 53
  • 88
Yooakim
  • 1,717
  • 2
  • 13
  • 21
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

Shaiju T
  • 6,201
  • 20
  • 104
  • 196