| 1 |
#!/usr/bin/bash |
| 2 |
|
| 3 |
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 |
# |
| 5 |
# Licensed under the MIT License. See the LICENSE accompanying this file |
| 6 |
# for the specific language governing permissions and limitations under |
| 7 |
# the License. |
| 8 |
|
| 9 |
function print_help() |
| 10 |
{ |
| 11 |
echo "ec2-metadata v0.1.2 |
| 12 |
Use to retrieve EC2 instance metadata from within a running EC2 instance. |
| 13 |
e.g. to retrieve instance id: ec2-metadata -i |
| 14 |
to retrieve ami id: ec2-metadata -a |
| 15 |
to get help: ec2-metadata --help |
| 16 |
For more information on Amazon EC2 instance meta-data, refer to the documentation at |
| 17 |
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html |
| 18 |
|
| 19 |
Usage: ec2-metadata <option> |
| 20 |
Options: |
| 21 |
--all Show all metadata information for this host (also default). |
| 22 |
-a/--ami-id The AMI ID used to launch this instance |
| 23 |
-l/--ami-launch-index The index of this instance in the reservation (per AMI). |
| 24 |
-m/--ami-manifest-path The manifest path of the AMI with which the instance was launched. |
| 25 |
-n/--ancestor-ami-ids The AMI IDs of any instances that were rebundled to create this AMI. |
| 26 |
-b/--block-device-mapping Defines native device names to use when exposing virtual devices. |
| 27 |
-i/--instance-id The ID of this instance |
| 28 |
-t/--instance-type The type of instance to launch. For more information, see Instance Types. |
| 29 |
-h/--local-hostname The local hostname of the instance. |
| 30 |
-o/--local-ipv4 Public IP address if launched with direct addressing; private IP address if launched with public addressing. |
| 31 |
-k/--kernel-id The ID of the kernel launched with this instance, if applicable. |
| 32 |
-z/--availability-zone The availability zone in which the instance launched. Same as placement |
| 33 |
-P/--partition The AWS partition name. |
| 34 |
-c/--product-codes Product codes associated with this instance. |
| 35 |
-p/--public-hostname The public hostname of the instance. |
| 36 |
-v/--public-ipv4 NATted public IP Address |
| 37 |
-u/--public-keys Public keys. Only available if supplied at instance launch time |
| 38 |
-r/--ramdisk-id The ID of the RAM disk launched with this instance, if applicable. |
| 39 |
-e/--reservation-id ID of the reservation. |
| 40 |
-s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time |
| 41 |
-d/--user-data User-supplied data.Only available if supplied at instance launch time. |
| 42 |
-g/--tags Tags assigned to this instance." |
| 43 |
} |
| 44 |
|
| 45 |
METADATA_BASEURL="http://169.254.169.254" |
| 46 |
METADATA_TOKEN_PATH="latest/api/token" |
| 47 |
QUIET="" |
| 48 |
|
| 49 |
function set_imds_token() |
| 50 |
{ |
| 51 |
if [ -z "${IMDS_TOKEN}" ];then |
| 52 |
IMDS_TOKEN=$(curl -s -f -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 900" ${METADATA_BASEURL}/${METADATA_TOKEN_PATH}) |
| 53 |
if [ "${?}" -gt 0 ] || [ -z "${IMDS_TOKEN}" ]; then |
| 54 |
echo '[ERROR] Could not get IMDSv2 token. Instance Metadata might have been disabled or this is not an EC2 instance.' |
| 55 |
exit 1 |
| 56 |
fi |
| 57 |
fi |
| 58 |
} |
| 59 |
|
| 60 |
# param1 = query |
| 61 |
function get_meta() |
| 62 |
{ |
| 63 |
local imds_out |
| 64 |
imds_out=$(curl -s -q -H "X-aws-ec2-metadata-token:${IMDS_TOKEN}" -f ${METADATA_BASEURL}/latest/${1}) |
| 65 |
echo -n "${imds_out}" |
| 66 |
} |
| 67 |
|
| 68 |
#print standard metric |
| 69 |
function print_normal_metric() { |
| 70 |
metric_path=$2 |
| 71 |
[ -z "$QUIET" ] && echo -n $1": " |
| 72 |
RESPONSE=$(get_meta ${metric_path}) |
| 73 |
if [ -n "${RESPONSE}" ]; then |
| 74 |
echo "$RESPONSE" |
| 75 |
else |
| 76 |
echo not available |
| 77 |
fi |
| 78 |
} |
| 79 |
|
| 80 |
#print block-device-mapping |
| 81 |
function print_block-device-mapping() |
| 82 |
{ |
| 83 |
[ -z "$QUIET" ] && echo 'block-device-mapping: ' |
| 84 |
x=$(get_meta meta-data/block-device-mapping/) |
| 85 |
if [ -n "${x}" ]; then |
| 86 |
for i in $x; do |
| 87 |
[ -z "$QUIET" ] && echo -ne '\t' "$i: " |
| 88 |
echo "$(get_meta meta-data/block-device-mapping/$i)" |
| 89 |
done |
| 90 |
else |
| 91 |
echo not available |
| 92 |
fi |
| 93 |
} |
| 94 |
|
| 95 |
#print public-keys |
| 96 |
function print_public-keys() |
| 97 |
{ |
| 98 |
[ -z "$QUIET" ] && echo 'public-keys: ' |
| 99 |
x=$(get_meta meta-data/public-keys/) |
| 100 |
if [ -n "${x}" ]; then |
| 101 |
for i in $x; do |
| 102 |
index=$(echo $i|cut -d = -f 1) |
| 103 |
keyname=$(echo $i|cut -d = -f 2) |
| 104 |
[ -z "$QUIET" ] && echo keyname:$keyname |
| 105 |
[ -z "$QUIET" ] && echo index:$index |
| 106 |
format=$(get_meta meta-data/public-keys/$index/) |
| 107 |
[ -z "$QUIET" ] && echo format:$format |
| 108 |
[ -z "$QUIET" ] && echo 'key:(begins from next line)' |
| 109 |
echo "$(get_meta meta-data/public-keys/$index/$format)" |
| 110 |
done |
| 111 |
else |
| 112 |
echo not available |
| 113 |
fi |
| 114 |
} |
| 115 |
|
| 116 |
#print tags |
| 117 |
function print_tags() |
| 118 |
{ |
| 119 |
[ -z "$QUIET" ] && echo 'tags: ' |
| 120 |
x=$(get_meta meta-data/tags/instance/) |
| 121 |
if [ -n "${x}" ]; then |
| 122 |
for i in $x; do |
| 123 |
echo -n -e '\t' "$i: " |
| 124 |
echo "$(get_meta meta-data/tags/instance/$i)" |
| 125 |
done |
| 126 |
else |
| 127 |
echo not available |
| 128 |
fi |
| 129 |
} |
| 130 |
|
| 131 |
function print_all() |
| 132 |
{ |
| 133 |
print_normal_metric ami-id meta-data/ami-id |
| 134 |
print_normal_metric ami-launch-index meta-data/ami-launch-index |
| 135 |
print_normal_metric ami-manifest-path meta-data/ami-manifest-path |
| 136 |
print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids |
| 137 |
print_block-device-mapping |
| 138 |
print_normal_metric instance-id meta-data/instance-id |
| 139 |
print_normal_metric instance-type meta-data/instance-type |
| 140 |
print_normal_metric local-hostname meta-data/local-hostname |
| 141 |
print_normal_metric local-ipv4 meta-data/local-ipv4 |
| 142 |
print_normal_metric kernel-id meta-data/kernel-id |
| 143 |
print_normal_metric placement meta-data/placement/availability-zone |
| 144 |
print_normal_metric partition meta-data/services/partition |
| 145 |
print_normal_metric product-codes meta-data/product-codes |
| 146 |
print_normal_metric public-hostname meta-data/public-hostname |
| 147 |
print_normal_metric public-ipv4 meta-data/public-ipv4 |
| 148 |
print_public-keys |
| 149 |
print_normal_metric ramdisk-id /meta-data/ramdisk-id |
| 150 |
print_normal_metric reservation-id /meta-data/reservation-id |
| 151 |
print_normal_metric security-groups meta-data/security-groups |
| 152 |
print_normal_metric user-data user-data |
| 153 |
print_tags |
| 154 |
} |
| 155 |
|
| 156 |
#check if run inside an EC2 instance |
| 157 |
set_imds_token |
| 158 |
|
| 159 |
#command called in default mode |
| 160 |
if [ "$#" -eq 0 ]; then |
| 161 |
print_all |
| 162 |
fi |
| 163 |
|
| 164 |
declare -a actions |
| 165 |
shortopts=almnbithokzPcpvuresdg |
| 166 |
longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping |
| 167 |
instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone |
| 168 |
partition product-codes public-hostname public-ipv4 public-keys ramdisk-id |
| 169 |
reservation-id security-groups user-data tags help all quiet) |
| 170 |
|
| 171 |
oldIFS="$IFS" |
| 172 |
IFS=, |
| 173 |
TEMP=$(getopt -o $shortopts --longoptions "${longopts[*]}" -n 'ec2-metadata' -- "$@") |
| 174 |
if [ $? -ne 0 ]; then |
| 175 |
echo 'Terminating...' >&2 |
| 176 |
exit 1 |
| 177 |
fi |
| 178 |
IFS="$oldIFS" |
| 179 |
|
| 180 |
eval set -- "$TEMP" |
| 181 |
unset TEMP |
| 182 |
|
| 183 |
while true; do |
| 184 |
case "$1" in |
| 185 |
--help) |
| 186 |
print_help ; shift |
| 187 |
exit 0 |
| 188 |
;; |
| 189 |
--quiet) |
| 190 |
QUIET=1 ; shift |
| 191 |
;; |
| 192 |
--) |
| 193 |
shift ; break |
| 194 |
;; |
| 195 |
--?*|-?) |
| 196 |
# pass most arguments to the original action processing |
| 197 |
# code after setting options |
| 198 |
actions+=("$1"); shift |
| 199 |
;; |
| 200 |
*) |
| 201 |
echo 'Unknown error: ' "[$1]" >&2 |
| 202 |
exit 1 |
| 203 |
;; |
| 204 |
esac |
| 205 |
done |
| 206 |
|
| 207 |
#start processing command line arguments |
| 208 |
for action in "${actions[@]}"; do |
| 209 |
case "$action" in |
| 210 |
-a | --ami-id ) print_normal_metric ami-id meta-data/ami-id ;; |
| 211 |
-l | --ami-launch-index ) print_normal_metric ami-launch-index meta-data/ami-launch-index ;; |
| 212 |
-m | --ami-manifest-path ) print_normal_metric ami-manifest-path meta-data/ami-manifest-path ;; |
| 213 |
-n | --ancestor-ami-ids ) print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids ;; |
| 214 |
-b | --block-device-mapping ) print_block-device-mapping ;; |
| 215 |
-i | --instance-id ) print_normal_metric instance-id meta-data/instance-id ;; |
| 216 |
-t | --instance-type ) print_normal_metric instance-type meta-data/instance-type ;; |
| 217 |
-h | --local-hostname ) print_normal_metric local-hostname meta-data/local-hostname ;; |
| 218 |
-o | --local-ipv4 ) print_normal_metric local-ipv4 meta-data/local-ipv4 ;; |
| 219 |
-k | --kernel-id ) print_normal_metric kernel-id meta-data/kernel-id ;; |
| 220 |
-z | --availability-zone ) print_normal_metric placement meta-data/placement/availability-zone ;; |
| 221 |
-P | --partition ) print_normal_metric partition meta-data/services/partition ;; |
| 222 |
-c | --product-codes ) print_normal_metric product-codes meta-data/product-codes ;; |
| 223 |
-p | --public-hostname ) print_normal_metric public-hostname meta-data/public-hostname ;; |
| 224 |
-v | --public-ipv4 ) print_normal_metric public-ipv4 meta-data/public-ipv4 ;; |
| 225 |
-u | --public-keys ) print_public-keys ;; |
| 226 |
-r | --ramdisk-id ) print_normal_metric ramdisk-id /meta-data/ramdisk-id ;; |
| 227 |
-e | --reservation-id ) print_normal_metric reservation-id /meta-data/reservation-id ;; |
| 228 |
-s | --security-groups ) print_normal_metric security-groups meta-data/security-groups ;; |
| 229 |
-d | --user-data ) print_normal_metric user-data user-data ;; |
| 230 |
-g | --tags ) print_tags ;; |
| 231 |
--all ) print_all; exit ;; |
| 232 |
esac |
| 233 |
shift |
| 234 |
done |
| 235 |
|
| 236 |
exit 0 |