项目地址

https://github.com/hnyapp/sh-cfddns
脚本日期:2020年6月2日
测试日期:2020年6月24日
测试结果:CentOS7.x下,VERY NICE!

cfupdater-v4

#!/bin/bash

# Forked by benkulbertis/cloudflare-update-record.sh
# CHANGE THESE
auth_email="john.appleseed@example.org"            # The email used to login 'https://dash.cloudflare.com'
auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3"   # Top right corner, "My profile" > "Global API Key"
zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
record_name=$1                                     # Which record you want to be synced

# DO NOT CHANGE LINES BELOW
ip=$(curl -s https://ipv4.icanhazip.com/)

# SCRIPT START
echo "[Cloudflare DDNS] $record_name, Check Initiated"

# Seek for the record
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=A" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")

# Can't do anything without the record
count=$(echo "$record" | grep -Po '(?<="count":)[0-9]*' | head -1)

if [[ $count == 0 ]]; then
  >&2 echo -e "[Cloudflare DDNS] $record_name, Record does not exist, perhaps create one first?"
  exit 1
fi

# Set existing IP address from the fetched record
old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

# Compare if they're the same
if [ $ip == $old_ip ]; then
  echo "[Cloudflare DDNS] $record_name, IP has not changed."
  exit 0
fi

# Set the record identifier from result
record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)

# The execution of update
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1}")

# The moment of truth
success=$(echo "$update" | grep -Po '(?<="success":)[^,]*' | head -1)

if [[ $success == "true" ]]; then
  echo "[Cloudflare DDNS] $record_name, IPv4 context '$ip' has been synced to Cloudflare."
else
  >&2 echo -e "[Cloudflare DDNS] $record_name, Update failed. DUMPING RESULTS:\n$update"
  exit 1
fi

cfupdater-v6

#!/bin/bash

# Forked by benkulbertis/cloudflare-update-record.sh
# CHANGE THESE
auth_email="john.appleseed@example.org"            # The email used to login 'https://dash.cloudflare.com'
auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3"   # Top right corner, "My profile" > "Global API Key"
zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
record_name=$1                                     # Which record you want to be synced

# DO NOT CHANGE LINES BELOW
ip=$(curl -s https://ipv6.icanhazip.com/)

# SCRIPT START
echo "[Cloudflare DDNS] $record_name, Check Initiated"

# Seek for the record
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=AAAA" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")

# Can't do anything without the record
count=$(echo "$record" | grep -Po '(?<="count":)[0-9]*' | head -1)

if [[ $count == 0 ]]; then
  >&2 echo -e "[Cloudflare DDNS] $record_name, Record does not exist, perhaps create one first?"
  exit 1
fi

# Set existing IP address from the fetched record
old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

# Compare if they're the same
if [ $ip == $old_ip ]; then
  echo "[Cloudflare DDNS] $record_name, IP has not changed."
  exit 0
fi

# Set the record identifier from result
record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)

# The execution of update
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip\",\"ttl\":1}")

# The moment of truth
success=$(echo "$update" | grep -Po '(?<="success":)[^,]*' | head -1)

if [[ $success == "true" ]]; then
  echo "[Cloudflare DDNS] $record_name, IPv6 context '$ip' has been synced to Cloudflare."
else
  >&2 echo -e "[Cloudflare DDNS] $record_name, Update failed. DUMPING RESULTS:\n$update"
  exit 1
fi
Last modification:June 24th, 2020 at 03:57 pm