-
Notifications
You must be signed in to change notification settings - Fork 10
/
ec2-metadata
executable file
·193 lines (181 loc) · 7.53 KB
/
ec2-metadata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# Copyright (C) 2006-2012 Amazon.com, Inc. or its affiliates.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the
# License.
function print_help()
{
echo "ec2-metadata v0.1.2
Use to retrieve EC2 instance metadata from within a running EC2 instance.
e.g. to retrieve instance id: ec2-metadata -i
to retrieve ami id: ec2-metadata -a
to get help: ec2-metadata --help
For more information on Amazon EC2 instance meta-data, refer to the documentation at
http://docs.amazonwebservices.com/AWSEC2/2008-05-05/DeveloperGuide/AESDG-chapter-instancedata.html
Usage: ec2-metadata <option>
Options:
--all Show all metadata information for this host (also default).
-a/--ami-id The AMI ID used to launch this instance
-l/--ami-launch-index The index of this instance in the reservation (per AMI).
-m/--ami-manifest-path The manifest path of the AMI with which the instance was launched.
-n/--ancestor-ami-ids The AMI IDs of any instances that were rebundled to create this AMI.
-b/--block-device-mapping Defines native device names to use when exposing virtual devices.
-i/--instance-id The ID of this instance
-t/--instance-type The type of instance to launch. For more information, see Instance Types.
-h/--local-hostname The local hostname of the instance.
-o/--local-ipv4 Public IP address if launched with direct addressing; private IP address if launched with public addressing.
-k/--kernel-id The ID of the kernel launched with this instance, if applicable.
-z/--availability-zone The availability zone in which the instance launched. Same as placement
-c/--product-codes Product codes associated with this instance.
-p/--public-hostname The public hostname of the instance.
-v/--public-ipv4 NATted public IP Address
-u/--public-keys Public keys. Only available if supplied at instance launch time
-r/--ramdisk-id The ID of the RAM disk launched with this instance, if applicable.
-e/--reservation-id ID of the reservation.
-s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time
-d/--user-data User-supplied data.Only available if supplied at instance launch time."
}
#check some basic configurations before running the code
function chk_config()
{
#check if run inside an ec2-instance
x=$(curl -sq http://169.254.169.254/)
if [ $? -gt 0 ]; then
echo '[ERROR] Command not valid outside EC2 instance. Please run this command within a running EC2 instance.'
exit 1
fi
}
#print standard metric
function print_normal_metric() {
metric_path=$2
echo -n $1": "
RESPONSE=$(curl -fsq http://169.254.169.254/latest/${metric_path}/)
if [ $? == 0 ]; then
echo $RESPONSE
else
echo not available
fi
}
#print block-device-mapping
function print_block-device-mapping()
{
echo 'block-device-mapping: '
x=$(curl -fsq http://169.254.169.254/latest/meta-data/block-device-mapping/)
if [ $? -eq 0 ]; then
for i in $x; do
echo -e '\t' $i: $(curl -sq http://169.254.169.254/latest/meta-data/block-device-mapping/$i)
done
else
echo not available
fi
}
#print public-keys
function print_public-keys()
{
echo 'public-keys: '
x=$(curl -fsq http://169.254.169.254/latest/meta-data/public-keys/)
if [ $? -eq 0 ]; then
for i in $x; do
index=$(echo $i|cut -d = -f 1)
keyname=$(echo $i|cut -d = -f 2)
echo keyname:$keyname
echo index:$index
format=$(curl -sq http://169.254.169.254/latest/meta-data/public-keys/$index/)
echo format:$format
echo 'key:(begins from next line)'
echo $(curl -sq http://169.254.169.254/latest/meta-data/public-keys/$index/$format)
done
else
echo not available
fi
}
function print_all()
{
print_normal_metric ami-id meta-data/ami-id
print_normal_metric ami-launch-index meta-data/ami-launch-index
print_normal_metric ami-manifest-path meta-data/ami-manifest-path
print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids
print_block-device-mapping
print_normal_metric instance-id meta-data/instance-id
print_normal_metric instance-type meta-data/instance-type
print_normal_metric local-hostname meta-data/local-hostname
print_normal_metric local-ipv4 meta-data/local-ipv4
print_normal_metric kernel-id meta-data/kernel-id
print_normal_metric placement meta-data/placement/availability-zone
print_normal_metric product-codes meta-data/product-codes
print_normal_metric public-hostname meta-data/public-hostname
print_normal_metric public-ipv4 meta-data/public-ipv4
print_public-keys
print_normal_metric ramdisk-id /meta-data/ramdisk-id
print_normal_metric reservation-id /meta-data/reservation-id
print_normal_metric security-groups meta-data/security-groups
print_normal_metric user-data user-data
}
#check if run inside an EC2 instance
chk_config
#command called in default mode
if [ "$#" -eq 0 ]; then
print_all
fi
#start processing command line arguments
while [ "$1" != "" ]; do
case $1 in
-a | --ami-id ) print_normal_metric ami-id meta-data/ami-id
;;
-l | --ami-launch-index ) print_normal_metric ami-launch-index meta-data/ami-launch-index
;;
-m | --ami-manifest-path ) print_normal_metric ami-manifest-path meta-data/ami-manifest-path
;;
-n | --ancestor-ami-ids ) print_normal_metric ancestor-ami-ids meta-data/ancestor-ami-ids
;;
-b | --block-device-mapping ) print_block-device-mapping
;;
-i | --instance-id ) print_normal_metric instance-id meta-data/instance-id
;;
-t | --instance-type ) print_normal_metric instance-type meta-data/instance-type
;;
-h | --local-hostname ) print_normal_metric local-hostname meta-data/local-hostname
;;
-o | --local-ipv4 ) print_normal_metric local-ipv4 meta-data/local-ipv4
;;
-k | --kernel-id ) print_normal_metric kernel-id meta-data/kernel-id
;;
-z | --availability-zone ) print_normal_metric placement meta-data/placement/availability-zone
;;
-c | --product-codes ) print_normal_metric product-codes meta-data/product-codes
;;
-p | --public-hostname ) print_normal_metric public-hostname meta-data/public-hostname
;;
-v | --public-ipv4 ) print_normal_metric public-ipv4 meta-data/public-ipv4
;;
-u | --public-keys ) print_public-keys
;;
-r | --ramdisk-id ) print_normal_metric ramdisk-id /meta-data/ramdisk-id
;;
-e | --reservation-id ) print_normal_metric reservation-id /meta-data/reservation-id
;;
-s | --security-groups ) print_normal_metric security-groups meta-data/security-groups
;;
-d | --user-data ) print_normal_metric user-data user-data
;;
-h | --help ) print_help
exit
;;
--all ) print_all
exit
;;
* ) print_help
exit 1
esac
shift
done