Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 382
Ehsan Zargar Ershadi Avatar asked Mar 04 '19 09:03

Ehsan Zargar Ershadi


2 Answers

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

It just shows like this:

enter image description here

like image 75
Charles Xu Avatar answered Sep 29 '22 10:09

Charles Xu


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.

like image 21
flags Avatar answered Sep 29 '22 10:09

flags