Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Azure/Amazon VM

Is there any way to detect, in general, if your code is executing on an Azure or Amazon virtual machine. I am not referring to some sort of web or worker role in particular, I mean given any executable, is there anything that resolves that machine to a cloud VM - for example under Azure there is no domain so i cannot simply rely on a domain name.

like image 652
user1371314 Avatar asked Jul 19 '12 23:07

user1371314


People also ask

How do I check if a VM is available in Azure?

Available VM Sizes (SKUs) by Azure Region Product Page Now to check which services are available in your preferred Azure region, I recommend that you check out the Products available by region page on Azure.com. If you search for virtual machines, you will find a list of Azure VM sizes available per region.

How can I see all virtual machines?

To see all VMs on the local Hyper-V host, you should run the Get-VM cmdlet. On the PowerShell screen, you can see the list of available VMs, including their name, state, CPU usage, memory assigned, uptime, status, and version.

Can you ping Azure public IP address?

In the Windows Firewall with Advanced Security, you can enable the Echo Request – ICMPv4-In or Echo Request ICMPv6-In rules, depending on if you need IPv4 or IPv6. After doing both steps, you should be able to ping your Azure Virtual Machine (VM) using a public IP address.


1 Answers

AWS

If your guest has networking up then you can probe the instance metadata by accessing http://169.254.169.254

For example:

$ curl http://169.254.169.254/1.0/meta-data/instance-id
i-87dc2f76

However, hitting the network is fairly heavy-weight.

On AWS you can also check by looking at dmidecode:

$ /usr/sbin/dmidecode -s bios-version | tr "[:upper:]" "[:lower:]" | grep -q "amazon"

dmidecode is light-weight since it only hits the memory of the guest OS. However, as pointed out in previous answers it is dependent on Amazon continuing to include the word "amazon" in their version strings.

Azure

On Azure, you can detect the hypervisor details but this doesn't allow you to discriminate between Azure and HyperV. Depending on your scenario this might not be necessary.

To detect Azure/HyperV using dmidecode check the following strings:

$ /usr/sbin/dmidecode -s system-manufacturer | tr "[:upper:]" "[:lower:]" | grep -q "microsoft corporation"

$ /usr/sbin/dmidecode -s system-product-name | tr "[:upper:]" "[:lower:]" | grep -q "virtual machine"
like image 166
Michael Richmond Avatar answered Sep 24 '22 05:09

Michael Richmond