Cloudflare DDNS 使用 API v4 重复 sh 脚本,在单个 sh 脚本中有多个 A 记录,但失败了



我正在尝试使用 Cloudflare API v4 在我的服务器上设置 DDNS。但我是编写文件的新手.sh。我希望在单个.sh文件中更新多个DNS记录。

我从互联网上得到了一个脚本(script1.sh(:

NEW_IP=`curl -s http://ipv4.icanhazip.com`
CURRENT_IP=`cat /Users/foo/Desktop/cloudflare/current_ip.txt`
if [ "$NEW_IP" = "$CURRENT_IP" ]
then
echo "No Change in IP Adddress"
else
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_record_id}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"{domain_name}","content":"'$NEW_IP'","ttl":1,"proxied":true}' 
echo $NEW_IP > /Users/foo/Desktop/cloudflare/current_ip.txt
fi

上述脚本适用于单个 DNS 记录更新,而不是像下面 (script2.sh( 那样的多记录更新:

NEW_IP=`curl -s http://ipv4.icanhazip.com`
CURRENT_IP=`cat /Users/foo/Desktop/cloudflare/current_ip.txt`
if [ "$NEW_IP" = "$CURRENT_IP" ]
then
echo "No Change in IP Adddress"
else
#domain-one
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id_for_domain_one}/dns_records/{dns_record_id_for_domain_one_record_one}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"domain-one.com","content":"'$NEW_IP'","ttl":1,"proxied":true}'
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id_for_domain_one}/dns_records/{dns_record_id_for_domain_one_record_two}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"subdomain.domain-one.com","content":"'$NEW_IP'","ttl":1,"proxied":true}'
#domain-two
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id_for_domain_two}/dns_records/{dns_record_id_for_domain_two_record}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"domain-two.com","content":"'$NEW_IP'","ttl":1,"proxied":true}'
echo $NEW_IP > /Users/foo/Desktop/cloudflare/current_ip.txt
fi

你能帮忙解释和解决问题吗?请告诉我剧本有什么问题。谢谢!

[编辑]我按sh /Users/foo/Desktop/script-name.sh运行一次,对于第一个示例(script1.sh(,还可以;对于第二个示例(script2.sh(,返回-bash: fork: Resource temporarily unavailable。由于我使用像cron这样的自动运行脚本,因此结果相同。

每个curl请求完成,需要添加&&&才能继续以下功能。

更多解释在这里。

NEW_IP=`curl -s http://ipv4.icanhazip.com`
CURRENT_IP=`cat /Users/foo/Desktop/cloudflare/current_ip.txt`
if [ "$NEW_IP" = "$CURRENT_IP" ]
then
echo "No Change in IP Adddress"
else
#domain-one
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id_for_domain_one}/dns_records/{dns_record_id_for_domain_one_record_one}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"domain-one.com","content":"'$NEW_IP'","ttl":1,"proxied":true}' &
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id_for_domain_one}/dns_records/{dns_record_id_for_domain_one_record_two}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"subdomain.domain-one.com","content":"'$NEW_IP'","ttl":1,"proxied":true}' &
#domain-two
curl -X PUT "https://api.cloudflare.com/client/v4/zones/{zone_id_for_domain_two}/dns_records/{dns_record_id_for_domain_two_record}" 
-H "X-Auth-Email: {my_email}" 
-H "X-Auth-Key: {global_api_key}" 
-H "Content-Type: application/json" 
--data '{"type":"A","name":"domain-two.com","content":"'$NEW_IP'","ttl":1,"proxied":true}'
echo $NEW_IP > /Users/foo/Desktop/cloudflare/current_ip.txt
fi

在浏览了许多关于这个主题的帖子之后,我最终从许多帖子中获取了部分内容,并确定了以下内容。

下面将遍历数组"dnsrecords"中的条目,并使用计算机的当前外部地址更新每个条目。

在此处查看更多信息,在此处以 cronjob 身份运行。

#!/usr/bin/bash
## Cloudflare authentication details
## Keep these private
cloudflare_auth_email=Your_Email
cloudflare_auth_key="Your_API_Token"
zoneid="Your_Zone_ID"
## Cloudflare Proxied DNS Records as Array
dnsrecords_proxied=(
"domain.com"
"www.domain.com"
"sub1.domain.com"
"sub2.domain.com"
"sub3.domain.com"
"sub3.domain.com"
)

## Cloudflare Non-Proxied DNS Records as Array
dnsrecords_not_proxied=(
"vpn01.domain.com"
"vpn02.domain.com"
)
## Files to log to (replace "path/to/" with script path)
log=path/to/log.log
log_ip=path/to/previous_ip
## Getting Date/Time
dt=$(date '+%d/%m/%Y %H:%M:%S')
## Get old IP from file
old_ip=$(cat $log_ip)
## Get the current external IP address
ip=$(curl -s -X GET https://api.ipify.org)
## Checking if IP changed since last update
if [[ ! $ip =~ ^[0-9]+.[0-9]+.[0-9]+.[0-9]+$ ]] || [ $ip = $old_ip ]; then
echo -en "$dt - Previous IP:$old_ipn$dt - Current  IP:$ipn$dt - No Changes Required....n" >> $log
echo "$(tail -n 1000 $log)" > $log
exit
## Exit if IP has not changed
else
## If the IP changed, not match the one on file "previous_ip"
echo -en "$dt - Previous IP:$old_ipn$dt - Current  IP:$ipn$dt - Starting Updates....n" >> $log
## Processing Proxied DNS Records
for dnsrecord in "${dnsrecords_proxied[@]}"
do
## For each DNS Record in Array "dnsrecords"
# Getting the DNS Record ID
dnsrecordid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=A&name=$dnsrecord" 
-H "X-Auth-Email: $cloudflare_auth_email" 
-H "Authorization: Bearer $cloudflare_auth_key" 
-H "Content-Type: application/json" | jq -r  '{"result"}[] | .[0] | .id') &&
# Updating the DNS Record
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordid" 
-H "X-Auth-Email: $cloudflare_auth_email" 
-H "Authorization: Bearer $cloudflare_auth_key" 
-H "Content-Type: application/json" 
--data "{"type":"A","name":"$dnsrecord","content":"$ip","ttl":1,"proxied":true}" | jq
echo -en "$dt - Updated - $dnsrecord n"  >> $log
done
## Processing Non Proxied DNS Records
for dnsrecord in "${dnsrecords_not_proxied[@]}"
do
## For each DNS Record in Array "dnsrecords"
# Getting the DNS Record ID
dnsrecordid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=A&name=$dnsrecord" 
-H "X-Auth-Email: $cloudflare_auth_email" 
-H "Authorization: Bearer $cloudflare_auth_key" 
-H "Content-Type: application/json" | jq -r  '{"result"}[] | .[0] | .id') &&
# Updating the DNS Record
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordid" 
-H "X-Auth-Email: $cloudflare_auth_email" 
-H "Authorization: Bearer $cloudflare_auth_key" 
-H "Content-Type: application/json" 
--data "{"type":"A","name":"$dnsrecord","content":"$ip","ttl":1,"proxied":false}" | jq
echo -en "$dt - Updated - $dnsrecord n"  >> $log
done
echo $ip > $log_ip
echo -en "$dt - Updates Completed.... n"  >> $log
echo "$(tail -n 1000 $log)" > $log
fi

最新更新