#!/usr/bin/env bashOLD_IP_NAME="Temp-IP"NEW_IP_NAME="Temp-IP"INSTANCE_NAME=""REGION=""CLOUDFLARE_ZONE_ID=""CLOUDFLARE_API_TOKEN=""# 定义多个 DNS 记录信息,每条记录格式为 "DNS_RECORD_ID,DNS_NAME"dns_records=("RECORD_ID_1,example1.com""RECORD_ID_2,example2.com""RECORD_ID_3,example3.com")echo"Detaching old Static IP: $OLD_IP_NAME"awslightsaildetach-static-ip\--static-ip-name"$OLD_IP_NAME"\--region"$REGION"\--profilelightsail\--no-cli-pager
echo"Releasing old Static IP: $OLD_IP_NAME"awslightsailrelease-static-ip\--static-ip-name"$OLD_IP_NAME"\--region"$REGION"\--profilelightsail\--no-cli-pager
echo"Allocating new Static IP: $NEW_IP_NAME"awslightsailallocate-static-ip\--static-ip-name"$NEW_IP_NAME"\--region"$REGION"\--profilelightsail\--no-cli-pager
echo"Attaching new Static IP: $NEW_IP_NAME to instance: $INSTANCE_NAME"awslightsailattach-static-ip\--static-ip-name"$NEW_IP_NAME"\--instance-name"$INSTANCE_NAME"\--region"$REGION"\--profilelightsail\--no-cli-pager
echo"Done. New Static IP has been attached."sleep2NEW_ALLOCATED_IP=$(awslightsailget-static-ip\--static-ip-name"$NEW_IP_NAME"\--region"$REGION"\--profilelightsail\--outputjson|jq-r'.staticIp.ipAddress')echo"New Static IP: $NEW_ALLOCATED_IP"# 循环更新每条 DNS 记录forrecordin"${dns_records[@]}";do# 根据逗号分隔,提取 DNS_RECORD_ID 和 DNS_NAMEIFS=','read-rDNS_RECORD_IDDNS_NAME<<<"$record"echo"Updating DNS record: $DNS_NAME (ID: $DNS_RECORD_ID)"curl"https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/$DNS_RECORD_ID"\-XPUT\-H"Authorization: Bearer $CLOUDFLARE_API_TOKEN"\-H"Content-Type: application/json"\-d'{ "content": "'"$NEW_ALLOCATED_IP"'", "name": "'"$DNS_NAME"'", "proxied": false, "ttl": 1, "type": "A" }'doneecho"Done. All Cloudflare DNS records have been updated."
$OLD_IP_NAME="Temp-IP"$NEW_IP_NAME="Temp-IP"$INSTANCE_NAME=""$REGION=""$CLOUDFLARE_ZONE_ID=""$CLOUDFLARE_API_TOKEN=""$dnsRecords=@(@{DNS_RECORD_ID="";DNS_NAME=""},@{DNS_RECORD_ID="";DNS_NAME=""},@{DNS_RECORD_ID="";DNS_NAME=""})Write-Host"Detaching old Static IP: $OLD_IP_NAME"awslightsaildetach-static-ip`--static-ip-name"$OLD_IP_NAME"`--region"$REGION"`--profilelightsail`--no-cli-pagerWrite-Host"Releasing old Static IP: $OLD_IP_NAME"awslightsailrelease-static-ip`--static-ip-name"$OLD_IP_NAME"`--region"$REGION"`--profilelightsail`--no-cli-pagerWrite-Host"Allocating new Static IP: $NEW_IP_NAME"awslightsailallocate-static-ip`--static-ip-name"$NEW_IP_NAME"`--region"$REGION"`--profilelightsail`--no-cli-pagerWrite-Host"Attaching new Static IP: $NEW_IP_NAME to instance: $INSTANCE_NAME"awslightsailattach-static-ip`--static-ip-name"$NEW_IP_NAME"`--instance-name"$INSTANCE_NAME"`--region"$REGION"`--profilelightsail`--no-cli-pagerWrite-Host"Done. New Static IP has been attached."Start-Sleep-Seconds2$NEW_ALLOCATED_IP=(awslightsailget-static-ip`--static-ip-name"$NEW_IP_NAME"`--region"$REGION"`--profilelightsail`--outputjson|ConvertFrom-Json).staticIp.ipAddressWrite-Host"New Static IP: $NEW_ALLOCATED_IP"$headers=@{"Authorization"="Bearer $CLOUDFLARE_API_TOKEN""Content-Type"="application/json"}# 循环更新每条 DNS 记录foreach($recordin$dnsRecords){$payload=@{content=$NEW_ALLOCATED_IPname=$record.DNS_NAMEproxied=$falsettl=1type ="A"}|ConvertTo-Json$uri="https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/$($record.DNS_RECORD_ID)"Write-Output"Updating DNS record: $($record.DNS_NAME) ($($record.DNS_RECORD_ID))"Invoke-RestMethod-Uri$uri-MethodPut-Headers$headers-Body$payload}Write-Output"Done. All Cloudflare DNS records have been updated."