@@ -40,23 +40,33 @@ Options:
4040-d/--user-data User-supplied data.Only available if supplied at instance launch time."
4141}
4242
43- # check some basic configurations before running the code
44- function chk_config()
43+ METADATA_BASEURL=" http://169.254.169.254"
44+ METADATA_TOKEN_PATH=" latest/api/token"
45+
46+ function set_imds_token()
4547{
46- # check if run inside an ec2-instance
47- x=$( curl -sq http://169.254.169.254/)
48- if [ $? -gt 0 ]; then
49- echo ' [ERROR] Command not valid outside EC2 instance. Please run this command within a running EC2 instance.'
50- exit 1
48+ if [ -z " ${IMDS_TOKEN} " ]; then
49+ IMDS_TOKEN=$( curl -s -f -X PUT -H " X-aws-ec2-metadata-token-ttl-seconds: 900" ${METADATA_BASEURL} /${METADATA_TOKEN_PATH} )
50+ if [ " ${?} " -gt 0 ] || [ -z " ${IMDS_TOKEN} " ]; then
51+ echo ' [ERROR] Could not get IMDSv2 token. Instance Metadata might have been disabled or this is not an EC2 instance.'
52+ exit 1
53+ fi
5154 fi
5255}
5356
57+ # param1 = query
58+ function get_meta()
59+ {
60+ local imds_out=$( curl -s -q -H " X-aws-ec2-metadata-token:${IMDS_TOKEN} " -f ${METADATA_BASEURL} /latest/${1} )
61+ echo -n ${imds_out}
62+ }
63+
5464# print standard metric
5565function print_normal_metric() {
5666 metric_path=$2
5767 echo -n $1 " : "
58- RESPONSE=$( curl -fsq http://169.254.169.254/latest/ ${metric_path} / )
59- if [ $? == 0 ]; then
68+ RESPONSE=$( get_meta ${metric_path} )
69+ if [ -n " ${RESPONSE} " ]; then
6070 echo " $RESPONSE "
6171 else
6272 echo not available
@@ -66,39 +76,38 @@ function print_normal_metric() {
6676# print block-device-mapping
6777function print_block-device-mapping()
6878{
69- echo ' block-device-mapping: '
70- x=$( curl -fsq http://169.254.169.254/latest/ meta-data/block-device-mapping/)
71- if [ $? -eq 0 ]; then
72- for i in $x ; do
73- echo -e ' \t' $i : $( curl -sq http://169.254.169.254/latest/ meta-data/block-device-mapping/$i )
74- done
75- else
76- echo not available
77- fi
79+ echo ' block-device-mapping: '
80+ x=$( get_meta meta-data/block-device-mapping/)
81+ if [ -n " ${x} " ]; then
82+ for i in $x ; do
83+ echo -e ' \t' $i : $( get_meta meta-data/block-device-mapping/$i )
84+ done
85+ else
86+ echo not available
87+ fi
7888}
7989
8090# print public-keys
8191function print_public-keys()
8292{
8393 echo ' public-keys: '
84- x=$( curl -fsq http://169.254.169.254/latest/ meta-data/public-keys/)
85- if [ $? -eq 0 ]; then
94+ x=$( get_meta meta-data/public-keys/)
95+ if [ -n " ${x} " ]; then
8696 for i in $x ; do
8797 index=$( echo $i | cut -d = -f 1)
8898 keyname=$( echo $i | cut -d = -f 2)
8999 echo keyname:$keyname
90100 echo index:$index
91- format=$( curl -sq http://169.254.169.254/latest/ meta-data/public-keys/$index /)
101+ format=$( get_meta meta-data/public-keys/$index /)
92102 echo format:$format
93103 echo ' key:(begins from next line)'
94- echo $( curl -sq http://169.254.169.254/latest/ meta-data/public-keys/$index /$format )
104+ echo $( get_meta meta-data/public-keys/$index /$format )
95105 done
96106 else
97107 echo not available
98108 fi
99109}
100110
101-
102111function print_all()
103112{
104113 print_normal_metric ami-id meta-data/ami-id
@@ -123,7 +132,7 @@ function print_all()
123132}
124133
125134# check if run inside an EC2 instance
126- chk_config
135+ set_imds_token
127136
128137# command called in default mode
129138if [ " $# " -eq 0 ]; then
0 commit comments