Configuring profile 'akProfile' in '' authenticate mode...
Access Key Id []: AccessKey ID
Access Key Secret []: AccessKey Secret
Default Region Id []: cn-hangzhou
Default Output Format [json]: json (Only support json))
Default Language [zh|en] en:
Saving profile[akProfile] ...Done.
非交互式配置
使用configure命令下的set子命令进行非交互式配置,配置命令如下:
1
2
3
4
5
6
aliyun configure set --profile akProfile
--mode AK
--region cn-hangzhou
--access-key-id AccessKeyId
--access-key-secret AccessKeySecret
注意
示例中的Default Region Id []: cn-hangzhou及--region cn-hangzhou写的是地域ID,这个示例是错误的,需要填写可用区ID,如cn-hangzhou-h。
#!/bin/bash
set -e
cd$(dirname $0)# 实例在几小时几分钟后过期。如果两者都不设置,默认3小时后过期,过期将自动释放实例。EXPIRE_HOURS=${1:-3}EXPIRE_MINUTES=${2:-0}# 启动模板ID,这里涉及个人信息,删除了具体值。START_TEMPLATE_ID=# 地域IDREGION_ID=cn-hongkong
# 创建实例后暂停几秒,再进行下一步操作。(实例初始化需要一些时间)INIT_SLEEP_SECONDS=22# 计时START_TIME=$(date +%s)ec(){echo"$(date +'%Y-%m-%d %H:%M:%S')${*}"}ec "instance will expire in ${EXPIRE_HOURS} hours ${EXPIRE_MINUTES} minutes"ec "Create instance starting..."# 创建实例CRETE_OUTPUT=$(aliyun ecs RunInstances --region ${REGION_ID} --RegionId "${REGION_ID}" --LaunchTemplateId "${START_TEMPLATE_ID}" --AutoReleaseTime `date -v+${EXPIRE_HOURS}H -v+${EXPIRE_MINUTES}M -u "+%Y-%m-%dT%H:%M:%SZ"`)ec "Instance info: ${CRETE_OUTPUT}"ec "Create instance executed for $(($(date +%s)- START_TIME)) seconds,completed."t1=$(date +%s)# 使用jq解析JSON并提取InstanceIdInstanceIdSet=$(echo"$CRETE_OUTPUT"| jq -rc '.InstanceIdSets.InstanceIdSet')# 检查InstanceIdSet是否已设置且非空if[ -z "$InstanceIdSet"];then ec "Error: The variable 'InstanceIdSet' is empty."exit1# 退出脚本,并返回错误码1fiec "The instance is created successfully. The instance id is as follows:"ec "$InstanceIdSet"ec "Public ip will be obtained soon, please wait a moment."ec "Will sleep ${INIT_SLEEP_SECONDS} seconds."sleep ${INIT_SLEEP_SECONDS}ec "About to get instance details..."t2=$(date +%s)# 获取实例详细信息DETAIL_OUTPUT=$(aliyun ecs DescribeInstances --region ${REGION_ID} --RegionId "'${REGION_ID}'" --InstanceIds "$InstanceIdSet" --read-timeout 30)t3=$(date +%s)ec "Instance details:"echo"${DETAIL_OUTPUT}"PUBLIC_IP=$(echo"$DETAIL_OUTPUT"| jq -rc '.Instances.Instance[0].PublicIpAddress.IpAddress[0]')ec "Instance public ip: ${PUBLIC_IP}"# 检查PUBLIC_IP是否已设置且非空if[ -z "$PUBLIC_IP"];then ec "Error: The variable 'PUBLIC_IP' is empty."exit1# 退出脚本,并返回错误码1fiec "Get public ip spend $((t3-t2))"# 设定重试次数RETRY_COUNT=10# 设定等待时间(秒)WAIT_TIME=3# 初始化计数器counter=0t4=$(date +%s)# PUBLIC_IP 公网IP地址while[$counter -lt $RETRY_COUNT];do ec "Instance initialization..."if ./pem.sh -i ${PUBLIC_IP} -k hk -f vpnclient.mobileconfig;thenbreakelseletcounter+=1 sleep $WAIT_TIMEfidoneif[$counter -eq $RETRY_COUNT];then ec "Error: Instance initialization time out."exit1fiEND_TIME=$(date +%s)ec "All task completed."echo"----------Statistics----------"echo"Create Instance took $((t1 - START_TIME)) seconds."echo"Obtaining the public IP took $((t3 - t2)) seconds."echo"Polling the PEM took $((END_TIME-t4)) seconds."echo"The instance initialization took $((END_TIME - t1)) seconds, which includes obtaining the public IP and polling the PEM file."echo"All tasks take a total of $((END_TIME - START_TIME)) seconds."