Amazon EC2 開機會自動取得自己 10.x.x.x 的 IP, 要抓取實體 IP / Hostname 等, 要用下述方法抓:
- 取得 IP:
wget -q -O /tmp/public-ip http://169.254.169.254/latest/meta-data/public-ipv4
- 取得 Hostname:
wget -q -O /tmp/public-hostname http://169.254.169.254/latest/meta-data/public-hostname
- 註: 169.254.169.254 是特殊 IP, 非 EC2 無法用此方法抓取.
下述取自: Distributed Ruby Workers on EC2
# Retrieve and store our ip / hostname
wget -q -O /tmp/public-ip http://169.254.169.254/latest/meta-data/public-ipv4
wget -q -O /tmp/public-hostname http://169.254.169.254/latest/meta-data/public-hostname
hostname -F /tmp/public-hostname
echo $(hostname) > /etc/hostname
相關網頁
- Feature Guide: Amazon EC2 User Selectable Kernels - 更多 http://169.254.169.254 的資訊取得
- Using Parameterized Launches to Customize Your AMIs - 於 EC2 開機時傳參數進去(curl http://169.254.169.254/1.0/user-data)
- Condor Workers on Amazon EC2