88
99function print_help()
1010{
11- echo " ec2-metadata v0.1.4
11+ echo " ec2-metadata v0.1.5
1212Use to retrieve EC2 instance metadata from within a running EC2 instance.
1313e.g. to retrieve instance id: ec2-metadata -i
1414 to retrieve ami id: ec2-metadata -a
@@ -41,11 +41,13 @@ Options:
4141-s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time
4242-d/--user-data User-supplied data.Only available if supplied at instance launch time.
4343-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."
4546}
4647
4748METADATA_BASEURL=" http://169.254.169.254"
4849METADATA_TOKEN_PATH=" latest/api/token"
50+ METADATA_VERSION=" latest"
4951QUIET=" "
5052
5153function set_imds_token()
@@ -63,7 +65,7 @@ function set_imds_token()
6365function get_meta()
6466{
6567 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} )
6769 echo -n " ${imds_out} "
6870}
6971
@@ -156,6 +158,27 @@ function print_all()
156158 print_tags
157159}
158160
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+
159182# check if run inside an EC2 instance
160183set_imds_token
161184
@@ -165,11 +188,12 @@ if [ "$#" -eq 0 ]; then
165188fi
166189
167190declare -a actions
191+ declare -a paths
168192shortopts=almnbithokzPcpvuresdgR
169193longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping
170194 instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone
171195 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: )
173197
174198oldIFS=" $IFS "
175199IFS=,
@@ -192,6 +216,11 @@ while true; do
192216 --quiet)
193217 QUIET=1 ; shift
194218 ;;
219+ --path)
220+ actions+=(" $1 " )
221+ paths+=(" $2 " )
222+ shift 2
223+ ;;
195224 --)
196225 shift ; break
197226 ;;
@@ -232,6 +261,7 @@ for action in "${actions[@]}"; do
232261 -s | --security-groups ) print_normal_metric security-groups meta-data/security-groups ;;
233262 -d | --user-data ) print_normal_metric user-data user-data ;;
234263 -g | --tags ) print_tags ;;
264+ --path ) print_path " ${paths[0]} " ; paths=(" ${paths[@]: 1} " ) ;;
235265 --all ) print_all; exit ;;
236266 esac
237267 shift
0 commit comments