Skip to content

Commit 87b7df1

Browse files
authored
Merge pull request #46 from q2ven/arbitrary_path
Suport arbitrary path for ec2-metadata.
2 parents 561948a + a4133d3 commit 87b7df1

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

doc/ec2-metadata.8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Print EC2 resource tags if permitted in EC2 Instance Metadata Options.
119119
.B \-\-quiet
120120
Don't print metadata keys
121121
.TP
122+
.B \-\-path
123+
Show metadata information from the specified path. Can be specified multiple times.
124+
.TP
122125
.B \-h, \-\-help
123126
Show summary of options.
124127
.SH SEE ALSO

ec2-metadata

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
function print_help()
1010
{
11-
echo "ec2-metadata v0.1.4
11+
echo "ec2-metadata v0.1.5
1212
Use to retrieve EC2 instance metadata from within a running EC2 instance.
1313
e.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

4748
METADATA_BASEURL="http://169.254.169.254"
4849
METADATA_TOKEN_PATH="latest/api/token"
50+
METADATA_VERSION="latest"
4951
QUIET=""
5052

5153
function set_imds_token()
@@ -63,7 +65,7 @@ function set_imds_token()
6365
function 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
160183
set_imds_token
161184

@@ -165,11 +188,12 @@ if [ "$#" -eq 0 ]; then
165188
fi
166189

167190
declare -a actions
191+
declare -a paths
168192
shortopts=almnbithokzPcpvuresdgR
169193
longopts=(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

174198
oldIFS="$IFS"
175199
IFS=,
@@ -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

Comments
 (0)