88
99function print_help()
1010{
11- echo " ec2-metadata v0.1.6
11+ echo " ec2-metadata v0.1.3
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
@@ -43,11 +43,13 @@ Options:
4343-s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time
4444-d/--user-data User-supplied data.Only available if supplied at instance launch time.
4545-g/--tags Tags assigned to this instance.
46- --quiet Suppress tag keys from the output."
46+ --quiet Suppress tag keys from the output.
47+ --path Show metadata information from the specified path. Can be specified multiple times."
4748}
4849
4950METADATA_BASEURL=" http://169.254.169.254"
5051METADATA_TOKEN_PATH=" latest/api/token"
52+ METADATA_VERSION=" latest"
5153QUIET=" "
5254
5355function set_imds_token()
@@ -65,7 +67,7 @@ function set_imds_token()
6567function get_meta()
6668{
6769 local imds_out
68- imds_out=$( curl -s -q -H " X-aws-ec2-metadata-token:${IMDS_TOKEN} " -f ${METADATA_BASEURL} /latest /${1} )
70+ imds_out=$( curl -s -q -H " X-aws-ec2-metadata-token:${IMDS_TOKEN} " -f ${METADATA_BASEURL} /${METADATA_VERSION} /${1} )
6971 echo -n " ${imds_out} "
7072}
7173
@@ -160,6 +162,27 @@ function print_all()
160162 print_tags
161163}
162164
165+ function print_path()
166+ {
167+ local path
168+
169+ path=$( echo " $1 " | sed ' s/\/\+/\//g' )
170+
171+ if [[ ! " $path " =~ (^/$| ^/? (1\. 0| [0-9]{4}-[0-9]{2}-[0-9]{2}| latest)) ]]; then
172+ if [[ " $path " =~ ^/? (dynamic| meta-data) ]]; then
173+ path=" latest/$path "
174+ else
175+ path=" latest/meta-data/$path "
176+ fi
177+ fi
178+
179+ path=$( echo " $path " | sed ' s/\/\+/\//g' )
180+
181+ METADATA_VERSION=" "
182+ print_normal_metric " $path " " $path "
183+ METADATA_VERSION=" latest"
184+ }
185+
163186# check if run inside an EC2 instance
164187set_imds_token
165188
@@ -169,11 +192,12 @@ if [ "$#" -eq 0 ]; then
169192fi
170193
171194declare -a actions
195+ declare -a paths
172196shortopts=almnDbithokzZPcpvuresdgR
173197longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids aws-domain block-device-mapping
174198 instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone availability-zone-id
175199 partition product-codes public-hostname public-ipv4 public-keys ramdisk-id
176- reservation-id security-groups user-data tags region help all quiet)
200+ reservation-id security-groups user-data tags region help all quiet path: )
177201
178202oldIFS=" $IFS "
179203IFS=,
@@ -196,6 +220,11 @@ while true; do
196220 --quiet)
197221 QUIET=1 ; shift
198222 ;;
223+ --path)
224+ actions+=(" $1 " )
225+ paths+=(" $2 " )
226+ shift 2
227+ ;;
199228 --)
200229 shift ; break
201230 ;;
@@ -238,6 +267,7 @@ for action in "${actions[@]}"; do
238267 -s | --security-groups ) print_normal_metric security-groups meta-data/security-groups ;;
239268 -d | --user-data ) print_normal_metric user-data user-data ;;
240269 -g | --tags ) print_tags ;;
270+ --path ) print_path " ${paths[0]} " ; paths=(" ${paths[@]: 1} " ) ;;
241271 --all ) print_all; exit ;;
242272 esac
243273 shift
0 commit comments