获取EC2实例信息写入登录提示文件/etc/motd
在管理EC2的过程中,需要做一些人性化的处理,如登录到EC2之后,会显示一些EC2相关的信息,如IP,所在节点,实例类型等,下面这个脚本就是获取这些信息。
# cat /usr/local/tools/ec2-metadata.sh
#!/bin/bash
#
function print_help()
{
echo “Directly use this to create /etc/motd to show instance info.”
}
#check some basic configurations before running the code
function chk_config()
{
#check if run inside an ec2-instance
x=$(curl -s 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 -ne “\033[33m$1:\t\033[0m”
RESPONSE=$(curl -fs http://169.254.169.254/latest/${metric_path}/)
if [ $? == 0 ]; then
echo -e “\033[35m$RESPONSE\033[0m”
else
echo “not available”
fi
}
#get hostname
function print_hostname(){
x=$(/bin/grep HOSTNAME /etc/sysconfig/network | /bin/awk -F “=” ‘{print $2}’)
if [ -n “$x” ]; then
echo -e “\033[33mHostname:\t\033[0m””\033[35m$x\033[0m”
else
echo “not available”
fi
}
function print_all()
{
echo “*************************************”
print_hostname
print_normal_metric Location meta-data/placement/availability-zone
print_normal_metric InstanceID meta-data/instance-id
print_normal_metric InstanceType meta-data/instance-type
print_normal_metric PrivateIP meta-data/local-ipv4
print_normal_metric PublicIP meta-data/public-ipv4
print_normal_metric VirtualTech meta-data/profile
echo “*************************************”
}
#check if run inside an EC2 instance
chk_config
#command called in default mode
if [ “$#” -eq 0 ]; then
print_all
fi
在/etc/profile中写入以下行,实现将获取的内容写入到motd文件中
/bin/bash /usr/local/tools/ec2-metadata.sh > /etc/motd
注意:之所以要写到profile文件,是因为EC2的信息很有可能会变,所以需要及时更新。