#!/usr/bin/env bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)get-cloud 3.2.2 12.11.2025 (c)2025 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Determine cloud platform and get metadata from instance if we are on oracle cloud. #@ #@Usage: get-platform [options] [key] #@ Options: -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -o, --output : Output format 'plain, 'keys' or 'json'. # # Possible values for cloud_id: # # ORACLE # ORACLE-OCI # ORACLE-OCI CLOUD-SHELL # ORACLE-OCI C@C # ORACLE-OPC # ORACLE-OPC C@C # MICROSOFT # DTAG # ... # # Update history: # # V 3.0.0 28.04.2020 New version # V 3.0.1 11.06.2020 Using library # V 3.0.2 02.07.2020 Check if we are running in OCI Cloud Shell # V 3.1.0 05.06.2023 New copyright # V 3.1.1 10.09.2023 Get metadata for oci # V 3.2.0 11.09.2024 New minor version # V 3.2.1 04.09.2025 Using hostnamectl # V 3.2.2 12.11.2025 Error in parameter check # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Set the Base URL opcurl="http://192.0.0.192" ociurl="http://169.254.169.254" asnurl="https://standby.cloud/cgi-bin/get-asn.pl?param" # Preset param="" cloudid="" asn="" check_c_at_c=0 formatstr="" # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -o | --output) shift if [ "$1" != "" ]; then if [ "$formatstr" = "" ]; then formatstr=`echo "$1" | tolower` if [ "$formatstr" != "plain" -a "$formatstr" != "keys" -a "$formatstr" != "json" ]; then errstr="Unknown format '$formatstr' after parameter '$pname'. Please choose from 'plain', 'keys' or 'json'." fi else errstr="Option '$pname' used more then once." fi shift else errstr="Please specify a format ('plain', 'keys' or 'json') after parameter '$pname'." fi ;; -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; *) shift paramck=`echo "$pname" | grep '^-'` # Keys don't begin with '-' if [ "$paramck" != "" ]; then errstr="Unknown option '$pname'." else if [ "$param" = "" -a "$paramck" = "" ]; then # Get the first parameter, squeeze '/' and delete any blanks # We can't lowercase the input because OCI uses mixed lower- and uppercase names param="`echo "$pname" | tr -d ' ' | sed 's|^/*||' | tr -s '/'`" else errstr="Unknown additional parameter: '$pname'." fi fi esac done # Set defaults if [ "$formatstr" = "" ]; then formatstr="plain" fi # Display help or error message DisplayHelp # Check if curl is in PATH curl=`filecheck -x curl` if [ "$curl" = "" ]; then exitcode=1 errormsg $exitcode "($progstr) No 'curl' in '$PATH'. Please install first e.g. 'sudo yum -y install curl'." else if [ "$param" = "" ]; then # Check if we have internet connection and determine asn asn=`get-ip asn` # Only check asn if we have an result (internet connection) if [ "$asn" != "" ]; then case "$asn" in # Microsoft AS12076 | AS13399 | AS14271 | AS14719 | AS20046 | AS23468 | AS35106 | AS3598 | AS395496 | AS395524 | AS395851 | \ AS396463 | AS45139 | AS52985 | AS5761 | AS6182 | AS6291 | AS6584 | AS8068 | AS8069 | AS8070 | AS8071) cloudid="MICROSOFT" ;; AS2914) # NTT cloudid="NTT" ;; AS1273) # Vodafone cloudid="VODAFONE" ;; AS12956) # Telefonica cloudid="TELEFONICA" ;; AS8560) # 1&1 IONOS cloudid="IONOS" ;; AS3320) # Deutsche Telekom cloudid="DTAG" ;; AS31898 | AS43898) # ORACLE-BMC-31898 cloudid="ORACLE-OCI" ;; # ORACLE-OPC # AS22435 PSFT-INC compute.us1.oraclecloud.com # AS4192 STORTEK-INT compute.us2.oraclecloud.com # AS7160 NETDYNAMICS compute.us6.oraclecloud.com # AS52019 ORCL-EMEA-AS login.em1.oraclecloud.com # AS43898 ORCL-AM-OPC1 compute.em2.oraclecloud.com # AS43894 ORCL-LON-OPC1 compute.gbcom-south-1.oraclecloud.com # AS4192 STORTEK-INT uscom-central-1.storage.oraclecloud.com AS22435 | AS4192 | AS7160 | AS52019 | AS43894 | AS4192) cloudid="ORACLE-OPC" ;; # ORACLE # AS792 ORACLE-ASNBLOCK-ASN oraclecloud.com # AS14506 ORCL-ASHBURN3 # AS63295 ORCL-CHICAGO1 # AS64102 Oracle Corporation # AS33517 DYNDNS AS792 | AS14506 | AS63295 | AS64102 | AS33517) cloudid="ORACLE" ;; *) # ASN lookup info: https://securitytrails.com/blog/asn-lookup # Add to url to get infos - add /prefixes to get all prefixes - add /peers to get all peers # cloudid=`transfer --quiet https://api.bgpview.io/asn/$asn | grep '"name":' | cut -d'"' -f4` cloudid=`transfer --quiet --seconds 5 "${asnurl}=$asn" | grep '\[AS' | cut -d']' -f2 | cut -d',' -f1` cloudid="${cloudid/ /}" esac if [ "$cloudid" = "ORACLE-OCI" -a "$OCI_CLI_CLOUD_SHELL" = "True" -a -f "/etc/bashrc.cloudshell" ]; then # We are in an OCI CloudShell and we can't use Metadata cloudid="ORACLE-OCI CLOUD-SHELL" fi if [ "$cloudid" != "" ]; then if [ "$formatstr" = "json" ]; then printf '{\n "cloud_id": "%s",\n "asn_id": "%s"' "$cloudid" "$asn" else if [ "$formatstr" = "keys" ]; then printf 'cloud_id:%s\nasn_id:%s\n' "$cloudid" "$asn" else printf 'cloud_id\nasn_id\n' fi fi if [ "$cloudid" != "ORACLE-OCI CLOUD-SHELL" ]; then if [ "$cloudid" != "ORACLE-OPC" -a "$cloudid" != "ORACLE-OCI" ]; then # Cloud@Customer with Internet Connection may has an other asn check_c_at_c=1 fi fi fi fi # Initialize ic="" mi="" bi="" hv="" mf="" pd="" vs="" # Check if we are running on a Hypervisor hostnamectl=`filecheck -x hostnamectl` if [ "$hostnamectl" != "" ]; then $hostnamectl > $scratchfile 2>&1 # hostnamectl -j | norm-json result=`filecheck -sl $scratchfile` if [ "$result" != "" ]; then ic=`cat $scratchfile | grep 'Icon name:' | cut -d':' -f2- | sed 's|^ *||'` mi=`cat $scratchfile | grep 'Machine ID:' | cut -d':' -f2- | sed 's|^ *||'` bi=`cat $scratchfile | grep 'Boot ID:' | cut -d':' -f2- | sed 's|^ *||'` hv=`cat $scratchfile | grep 'Virtualization:' | cut -d':' -f2- | sed 's|^ *||' | toupper` mf=`cat $scratchfile | grep 'Hardware Vendor:' | cut -d':' -f2- | sed 's|^ *||'` pd=`cat $scratchfile | grep 'Hardware Model:' | cut -d':' -f2- | sed 's|^ *||'` vs=`cat $scratchfile | grep 'Firmware Version:' | cut -d':' -f2- | sed 's|^ *||'` fi else dmesg=`filecheck -x dmesg` if [ "$dmesg" != "" ]; then hv=`$dmesg 2>/dev/null | grep -i ' Hypervisor detected: ' | cut -d':' -f2- | sed 's|^ *||'` if [ "$hv" != "" ]; then filecheck -rm $scratchfile dmidecode=`filecheck -x dmidecode` if [ "$dmidecode" != "" ]; then if [ "$USER" = "root" ]; then $dmidecode -t system > $scratchfile 2>&1 else check-sudo stat=$? if [ $stat -eq 0 ]; then sudo $dmidecode -t system > $scratchfile 2>&1 fi fi fi result=`filecheck -sl $scratchfile` if [ "$result" != "" ]; then mf=`cat $scratchfile | grep 'Manufacturer:' | cut -d':' -f2- | sed 's|^ *||'` pd=`cat $scratchfile | grep 'Product Name:' | cut -d':' -f2- | sed 's|^ *||'` vs=`cat $scratchfile | grep 'Version:' | cut -d':' -f2- | sed 's|^ *||'` fi fi fi fi if [ "$formatstr" = "json" ]; then printf ',\n "hypervisor": "%s"' "$hv" if [ "$mf" != "" ]; then printf ',\n "manufacturer": "%s"' "$mf" fi if [ "$pd" != "" ]; then printf ',\n "product_name": "%s"' "$pd" fi if [ "$vs" != "" ]; then printf ',\n "version": "%s"' "$vs" fi if [ "$ic" != "" ]; then printf ',\n "icon_name": "%s"' "$ic" fi if [ "$mi" != "" ]; then printf ',\n "machine_id": "%s"' "$mi" fi if [ "$bi" != "" ]; then printf ',\n "boot_id": "%s"' "$bi" fi else if [ "$formatstr" = "keys" ]; then printf 'hypervisor:%s\n' "$hv" if [ "$mf" != "" ]; then printf 'manufacturer:%s\n' "$mf" fi if [ "$pd" != "" ]; then printf 'product_name:%s\n' "$pd" fi if [ "$vs" != "" ]; then printf 'version:%s\n' "$vs" fi if [ "$ic" != "" ]; then printf 'icon_name:%s\n' "$ic" fi if [ "$mi" != "" ]; then printf 'machine_id:%s\n' "$mi" fi if [ "$bi" != "" ]; then printf 'boot_id:%s\n' "$bi" fi else printf 'hypervisor\n' if [ "$mf" != "" ]; then printf 'manufacturer\n' fi if [ "$pd" != "" ]; then printf 'product_name\n' fi if [ "$vs" != "" ]; then printf 'version\n' fi if [ "$ic" != "" ]; then printf 'icon_name\n' fi if [ "$mi" != "" ]; then printf 'machine_id\n' fi if [ "$bi" != "" ]; then printf 'boot_id\n' fi fi fi if [ "$formatstr" = "json" ]; then printf '\n}\n' fi else lparam=`echo $param | cut -d'/' -f1` rparam=`echo $param | cut -d'/' -f2-` if [ "$lparam" = "$rparam" ]; then rparam="" fi # echo "param: '$param'." # echo "lparam: '$lparam'." # echo "rparam: '$rparam'." if [ "$lparam" = "opc" ]; then if [ "$rparam" = "" ]; then transfer --quiet --seconds 5 ${opcurl}/ --export $scratchfile stat=$? if [ $stat -eq 0 -a -f $scratchfile ]; then printf '[\n' while read -r line; do if [ "$line" = "latest" ]; then printf ' "%s"\n' $line else printf ' "%s",\n' $line fi done < $scratchfile printf ']\n' else exitcode=2 fi else echo "OPC check '$rparam'." fi else if [ "$lparam" = "oci" ]; then transfer --auth --quiet --seconds 5 ${ociurl}/opc/v2/instance/ --export $scratchfile stat=$? if [ $stat -eq 0 -a -f $scratchfile ]; then cat $scratchfile | browse-json "$rparam" --raw --select 1 --output $formatstr else exitcode=2 fi else get-cloud --output keys > $scratchfile if [ -s $scratchfile ]; then grepres=`grep "^${lparam}:" $scratchfile | cut -d':' -f2` if [ "$grepres" != "" ]; then echo "$grepres" else exitcode=3 errormsg $exitcode "($progstr) Unknown cloud type (oci or opc) or key '$lparam'." fi fi fi fi fi fi # Cleanup filecheck -rm $scratchfile exit $exitcode