azure

Obtain Public IP from an Azure VM via CLI

You are automating your stuff and need to obtain the the public IP from an Azure VM, via CLI. Here’s some tips and a step by step for you.

Considerations:

  • You are using Linux|Unix
  • You are using CLI
  • You have access to the azure environment and a running VM.

TL;DR

  1. Install Azure CLI;
  2. Get public IP;
$ curl -L https://aka.ms/InstallAzureCli | bash
$ source ~/.bash_profile
$ az vm show -d -g MyGroupName1 -n MyVMName1 --query publicIps -o tsv

Detailed Instructions:

Installing Azure CLI

If you recently started working with Azure you first need to install Azure CLI on your environment.

1. Open your terminal and run:

curl -L https://aka.ms/InstallAzureCli | bash

If you don’t want to change the installation directories, just press enter whenever queried. Watch it download and install dependencies.

2. Refresh the settings available in your bash_profile configuration (or any other file you chose during installation) with the command below (or open a new tab).

source ~/.bash_profile

3. Log in into the azure account you want to work with.

Option 1 – Non-Interactive:

az login -u <username> -p <password>

Option 2 – Interactive: When running the command below, a web page should open in your browser.

az login

Select the a user within in the web page that just opened.

Get back to your terminal and you should now be able to access your resources.

Obtaining Azure VM Public IP via CLI

In azure, the virtual machines and all other resources belongs to a group. If you don’t remember the names of your groups, run this command to get a list of their names:

$ az group list --query [].name
 [
   "MyGroupName1",
   "MyGroupName2"
 ]

To grab the name of your VM, you can run:

$ az vm list --query [].name
[
   "MyVMName1",
   "MyVMName2"
 ]

Now, you can finally get your public IP (fake result exemplified below):

$ az vm show -d -g MyGroupName1 -n MyVMName1 --query publicIps -o tsv 
199.222.222.100

That’s it. :]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s