AWS国际 lightsail 创建及管理shell脚本
使用说明:需安装ACL
#!/bin/bash
function configure(){
if [[ -f /etc/aws.conf ]]; then
echo -e "你已经登录过aws密钥存放于/etc/aws.conf"
AccessKeyId=`grep "AK" /etc/aws.conf | grep -oP '(?<==).*'`
SecretKey=`grep "SK" /etc/aws.conf | grep -oP '(?<==).*'`
else
echo && stty erase '^H' && read -p "输入你的AccessKeyId: " AccessKeyId
echo && stty erase '^H' && read -p "输入你的SecretKey: " SecretKey
echo -e "AK=${AccessKeyId}\nSK=${SecretKey}" > /etc/aws.conf
fi
aws configure <<EOF
${AccessKeyId}
${SecretKey}
${region}
EOF
echo '1' > /etc/aws.lock
}
function setzone(){
echo -e "
1)日本-东京
2)新加坡
3)德国-法兰克福
4)英国-伦敦
5)爱尔兰
6)印度-孟买
7)澳大利亚-悉尼
8)美东1-弗吉尼亚
9)美东2-俄亥俄
10)美西2-俄勒冈
11)韩国-首尔
12)法国-巴黎
13)加拿大-蒙特利尔
"
echo && stty erase '^H' && read -p "需要操作实例地区(确定你有配额创建): " zonen
case "$zonen" in
1)
zone="ap-northeast-1a"
;;
2)
zone="ap-southeast-1a"
;;
3)
zone="eu-central-1a"
;;
4)
zone="eu-west-2a"
;;
5)
zone="eu-west-1a"
;;
6)
zone="ap-south-1a"
;;
7)
zone="ap-southeast-2a"
;;
8)
zone="us-east-1a"
;;
9)
zone="us-east-2a"
;;
10)
zone="us-west-2a"
;;
11)
zone="ap-northeast-2a"
;;
12)
zone="eu-west-3a"
;;
13)
zone="ca-central-1a"
;;
*)
echo -e "please enter the right number [1-10]"
;;
esac
region=`echo ${zone%*?}`
configure
Options
}
setConf(){
createDate=`date +%s`
stty erase '^H' &&read -p "请输入所需创建实例数量:" num
stty erase '^H' &&read -p "请输入实例名:" instancename
stty erase '^H' &&read -p "请输入Root密码:" rootpasswd
setzone
stty erase '^H' &&read -p "请输入开放起始端口:" startPort
stty erase '^H' &&read -p "请输入开放结束端口:" endPort
OsId="centos_7_1901_01"
size="nano_2_0"
}
#定义函数批量创建实例,并创建静态IP且绑定到同名实例,同时开启指定安全组端口
batchCreateVps(){
for ((i=1;i < $num+1; i++))
do
aws lightsail create-instances --instance-name ${instancename}-${createDate}-${i} --availability-zone ${zone} --blueprint-id ${OsId} --bundle-id ${size} --user-data "echo root:${rootpasswd} |sudo chpasswd &&sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config&&sudo systemctl restart sshd"
done
echo "实例创建中..."
sleep 5
for ((i=1;i < $num+1; i++))
do
aws lightsail open-instance-public-ports --port-info fromPort=${startPort},toPort=${endPort},protocol=TCP --instance-name ${instancename}-${createDate}-$i
aws lightsail open-instance-public-ports --port-info fromPort=${startPort},toPort=${endPort},protocol=UDP --instance-name ${instancename}-${createDate}-$i
aws lightsail allocate-static-ip --static-ip-name ${instancename}-${createDate}-$i-IP
aws lightsail attach-static-ip --static-ip-name ${instancename}-${createDate}-$i-IP --instance-name ${instancename}-${createDate}-$i
done
Options
}
startOrStopVps(){
stty erase '^H' &&read -p "请输选择操作是否单个实例[ y or n ]:" Bool
if [[ $Bool == y ]]; then
stty erase '^H' &&read -p "请输入实例名:" instancename
aws lightsail stop-instance --instance-name $instancename
else
instancename=(`awk '{print $2}' pendingvpslist`)
for((i=0; i<${#instancename[*]}; i++))
do
echo ${instancename[$i]}
aws lightsail stop-instance --instance-name ${instancename[$i]}
if [[ -z “${instancename[$i]}” ]]; then
break
fi
done
fi
Options
}
Options(){
clear
echo -e "
1 创建实例
2 操作实例
3 重新选择区域
当前区域 $zone
"
stty erase '^H' &&read -p "请输选择要执行的操作:" option
}
Options
case $option in
1)
setConf
batchCreateVps
;;
2)
startOrStopVps
;;
3)
setzone
;;
*)
echo err!!!
esac