Skip to content

Instantly share code, notes, and snippets.

@aaronfuj
Created May 20, 2023 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronfuj/001d73ee323420ff5f74182935435722 to your computer and use it in GitHub Desktop.
Save aaronfuj/001d73ee323420ff5f74182935435722 to your computer and use it in GitHub Desktop.
A bash script to be ran within a Cloud Service Provider's VM to identify the type of Cloud Provider and IMDS service used.
#!/usr/bin/env bash
set -e pipefail
function getCloudProvider() {
if curl --fail -s -m 5 http://169.254.169.254/latest/meta-data/placement/availability-zone > /dev/null; then
echo "aws_imdsv1"
elif TOKEN=`curl --fail -s -m 5 -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 600"` \
&& curl --fail -s -m 5 -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone > /dev/null; then
echo "aws_imdsv2"
elif curl --fail -s -m 5 -H "Metadata:true" "http://169.254.169.254/metadata/instance?api-version=2017-08-01" > /dev/null; then
echo "azure"
elif curl --fail -s -m 5 -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/project/ > /dev/null; then
echo "gcp"
elif curl --fail -s -m 5 -H "Authorization: Bearer Oracle" -L http://169.254.169.254/opc/v2/instance/ > /dev/null; then
echo "oci"
else
echo "unknown"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment