8
8
9
9
function print_help()
10
10
{
11
- echo " ec2-metadata v0.1.4
11
+ echo " ec2-metadata v0.1.5
12
12
Use to retrieve EC2 instance metadata from within a running EC2 instance.
13
13
e.g. to retrieve instance id: ec2-metadata -i
14
14
to retrieve ami id: ec2-metadata -a
@@ -41,11 +41,13 @@ Options:
41
41
-s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time
42
42
-d/--user-data User-supplied data.Only available if supplied at instance launch time.
43
43
-g/--tags Tags assigned to this instance.
44
- --quiet Suppress tag keys from the output."
44
+ --quiet Suppress tag keys from the output.
45
+ --path Show metadata information from the specified path. Can be specified multiple times."
45
46
}
46
47
47
48
METADATA_BASEURL=" http://169.254.169.254"
48
49
METADATA_TOKEN_PATH=" latest/api/token"
50
+ METADATA_VERSION=" latest"
49
51
QUIET=" "
50
52
51
53
function set_imds_token()
@@ -63,7 +65,7 @@ function set_imds_token()
63
65
function get_meta()
64
66
{
65
67
local imds_out
66
- imds_out=$( curl -s -q -H " X-aws-ec2-metadata-token:${IMDS_TOKEN} " -f ${METADATA_BASEURL} /latest /${1} )
68
+ imds_out=$( curl -s -q -H " X-aws-ec2-metadata-token:${IMDS_TOKEN} " -f ${METADATA_BASEURL} /${METADATA_VERSION} /${1} )
67
69
echo -n " ${imds_out} "
68
70
}
69
71
@@ -156,6 +158,27 @@ function print_all()
156
158
print_tags
157
159
}
158
160
161
+ function print_path()
162
+ {
163
+ local path
164
+
165
+ path=$( echo " $1 " | sed ' s/\/\+/\//g' )
166
+
167
+ if [[ ! " $path " =~ (^/$| ^/? (1\. 0| [0-9]{4}-[0-9]{2}-[0-9]{2}| latest)) ]]; then
168
+ if [[ " $path " =~ ^/? (dynamic| meta-data) ]]; then
169
+ path=" latest/$path "
170
+ else
171
+ path=" latest/meta-data/$path "
172
+ fi
173
+ fi
174
+
175
+ path=$( echo " $path " | sed ' s/\/\+/\//g' )
176
+
177
+ METADATA_VERSION=" "
178
+ print_normal_metric " $path " " $path "
179
+ METADATA_VERSION=" latest"
180
+ }
181
+
159
182
# check if run inside an EC2 instance
160
183
set_imds_token
161
184
@@ -165,11 +188,12 @@ if [ "$#" -eq 0 ]; then
165
188
fi
166
189
167
190
declare -a actions
191
+ declare -a paths
168
192
shortopts=almnbithokzPcpvuresdgR
169
193
longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping
170
194
instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone
171
195
partition product-codes public-hostname public-ipv4 public-keys ramdisk-id
172
- reservation-id security-groups user-data tags region help all quiet)
196
+ reservation-id security-groups user-data tags region help all quiet path: )
173
197
174
198
oldIFS=" $IFS "
175
199
IFS=,
@@ -192,6 +216,11 @@ while true; do
192
216
--quiet)
193
217
QUIET=1 ; shift
194
218
;;
219
+ --path)
220
+ actions+=(" $1 " )
221
+ paths+=(" $2 " )
222
+ shift 2
223
+ ;;
195
224
--)
196
225
shift ; break
197
226
;;
@@ -232,6 +261,7 @@ for action in "${actions[@]}"; do
232
261
-s | --security-groups ) print_normal_metric security-groups meta-data/security-groups ;;
233
262
-d | --user-data ) print_normal_metric user-data user-data ;;
234
263
-g | --tags ) print_tags ;;
264
+ --path ) print_path " ${paths[0]} " ; paths=(" ${paths[@]: 1} " ) ;;
235
265
--all ) print_all; exit ;;
236
266
esac
237
267
shift
0 commit comments