示例代码:
以下示例代码可以帮助您检查EC2实例是否正常运行,各个组件是否设置正确。
#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
LOCAL_IP_ADDR=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
echo "Instance ID: $INSTANCE_ID"
echo "Local IP Address: $LOCAL_IP_ADDR"
echo "Checking if EC2 instance is running ..."
if [ $(curl -Is http://169.254.169.254/latest/meta-data/instance-id | head -n 1 | grep "200 OK" | wc -l) -gt 0 ]; then
echo "EC2 instance is running ..."
else
echo "EC2 instance is not running ..."
exit 1
fi
echo "Checking if network connectivity is working ..."
if [ $(ping -c 1 8.8.8.8 | grep "1 received" | wc -l) -gt 0 ]; then
echo "Connectivity to 8.8.8.8 is working ..."
else
echo "No connectivity to 8.8.8.8 ..."
exit 1
fi
echo "Checking if security group settings are correct ..."
if [ $(aws ec2 describe-security-groups --group-id | grep -e <