#!/bin/bash
# Setup the default subscription if you have never done this before!!
az account set --subscription bd....7 

# create resource group
az group create --name myvm-resource-gp --location ukwest

# Create the vNet and subnet, since this is the first vNet create it becomes the default
az network vnet create --name myVNet --resource-group myvm-resource-gp  --subnet-name mySubnet --address-prefix 10.1.0.0/16  --subnet-prefix 10.1.1.0/24

# Display the virtual networks in our resource group
az network vnet list --resource-group myvm-resource-gp --output table

# Create a public IP address. This actually separate Azure resource and incurs a small cost
az network public-ip create --resource-group myvm-resource-gp --name myPublicIP --allocation-method Static 

# Create the NIC 
az network nic create --location ukwest --resource-group myvm-resource-gp --name myNic --vnet-name myVNet --subnet mySubnet --public-ip-address myPublicIP 

# Create the VM with default size Standard_DS1_v2 using the NIC
# It's a Unix VM so we will generate SSH keys to allow us to login to it.
# DO THIS IN THE WEB PORTAL OR THE SSH KEYS WILL NOT BE STORED 
az vm create --resource-group myvm-resource-gp --name myVM --nics myNic --image UbuntuLTS --admin-username azureuser --generate-ssh-keys

# Show VM details. Check out NIC, vNet, subnet and public IP
az vm show --resource-group myvm-resource-gp --name myVM --show-details 

# Create a second VM using the default vNet/subnet we created
# The NIC is automatically created.
az vm create --resource-group myvm-resource-gp --name myVM2 --image UbuntuLTS --admin-username azureuser --generate-ssh-keys

# Create a third VM but be very specific about the vNet and subnet it should be attached to.
# The NIC is automatically created.
az vm create --resource-group myvm-resource-gp --name myVM3 --vnet-name myVNet --subnet mySubnet --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
 
# List the current NICs, MAC and private IP addresses
az network nic list --resource-group myvm-resource-gp --query "[].{Name:name, MAC:macAddress, IP:ipConfigurations[0].privateIpAddress}" --output table

# Get the public IP of the first VM (to which we added the NIC) and SSH into it
az vm show --resource-group myvm-resource-gp --name myVM --show-details --query publicIps

# SSH into one of the VMs public IP address and ping the others by name and private IP address
ssh azureuser@20.90.8.168
# NOTICE THE CONNECTION TIMESOUT!!

# Create a NSG, a NSG rule and then add the NSG to the NIC
az network nsg create --resource-group myvm-resource-gp --name sshNsg
az network nsg rule create --resource-group myvm-resource-gp --nsg-name sshNsg --name allow-SSH --protocol tcp --priority 100 --destination-address-prefixes '*' --destination-port-ranges 22 --source-address-prefixes '*' --source-port-ranges '*' --access Allow
az network nic update --resource-group myvm-resource-gp --name myNic --network-security-group sshNsg

# SSH into one of the VMs public IP address and ping the others by name and private IP address
ssh azureuser@20.90.8.168
ifconfig  # display the private IP address
ping myVM2
ping 123.38.4.5 # private IP of myVM2

# Try one of the other VMs you created
# It works because a default NSG is automatically created fo these VMs with a default rule allowing SSH
az vm show --resource-group myvm-resource-gp --name myVM2 --show-details --query publicIps
ssh azureuser@20.90.8.169

# Clean up
az group delete --name myvm-resource-gp --yes

##################################################################################################
# If you are not in the web portal CLI then you will need to generate an SSH key
# in a Bash shell using the azure CLI...the results look like this:
# Private key is saved to "/home/user/.ssh/7777777777_9999999".
# Public key is saved to "/home/user/.ssh/7777777777_9999999.pub".
# These files are stored in the directory you are current 
az sshkey create --name mySSHKey --resource-group myvm-resource-gp
chmod 600 /home/user/.ssh/7777777777_9999999
# Now connect
ssh -identity_file /home/user/.ssh/7777777777_9999999 username@<ipaddress of the VM>
# Upload public key to store in Azure
az sshkey create --name "mySSHKey" --public-key "@/home/user/.ssh/7777777777_9999999.pub" --resource-group "myResourceGroup"
See https://learn.microsoft.com/en-us/azure/virtual-machines/ssh-keys-azure-cli
##################################################################################################
