Skip to content

Instantly share code, notes, and snippets.

@crotel
Last active March 22, 2020 05:58
Show Gist options
  • Save crotel/a8c00c73dd248a655cc9dadf5690173c to your computer and use it in GitHub Desktop.
Save crotel/a8c00c73dd248a655cc9dadf5690173c to your computer and use it in GitHub Desktop.
Cloudflare Dynamic Dns with custome api token. with little changed. let log file would not too big.
[Unit]
Description=Cloudflare Dynamic DNS service
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
StandardOutput=journal
StandardError=journal
# CHANGE THESE VALUES
User=yourusername
ExecStart=/yourlocal/ddns.sh
WorkingDirectory=/yourlocal/
#!/bin/bash
#
# Cloudflare Dynamic DNS #
#
# For secure reason, this shell using Cloudflare custome API Tokens, Gobal API will not run.#
#
# CHANGE #
auth_email="your@email.com" # your cloudflare login email
api_token="your-custome-api-token" # your custome api Tokens, profile -> api-tokens -> create Token.
zone_name="yourdomain.com" # your domain.
zone_identifier="your-zone-id" # your zone id, choose domain, in overview panel -> right-bottom corner below 'API' captal - > Zone ID
record_name="ddns.yourdomain.com" # your subdomain, recommend using 'ddns' or 'dynamic' for subdomain.
#
# create api Token sample #
# sample begin #
# Token name -> 'dynamic'
# Permissions -> 'zone' 'DNS' 'edit'
# Account Resources -> 'include' 'All accounts'
# Zone Resources -> 'Include' 'Specific' 'zone' 'yourdomain'
# sample end #
#
# CHANGE IN YOUR WAY OR JUST LET IT BE #
ip=$(curl -s http://ipv4.icanhazip.com)
ip_file="ip.txt"
id_file="cloudflare.ids"
log_file="cloudflare.log"
#
# LOGGER
log() {
if [ "$1" ]; then
echo -e "[$(date)] - $1" > $log_file
fi
}
#
# SCRIPT START
#
if [ -f $ip_file ]; then
old_ip=$(cat $ip_file)
if [ $ip == $old_ip ]; then
message="Check Initiated, IP has not changed."
log "$message"
echo -e "$message"
exit 0
fi
fi
#
# GET record_identifier #
if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 1 ]; then
record_identifier=$(head -1 $id_file)
else
record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
echo "$record_identifier" > $id_file
fi
#
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Em
ail: $auth_email" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"
type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")
#
if [[ $update == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
log "$message"
echo -e "$message"
exit 1
else
message="IP changed to: $ip"
echo "$ip" > $ip_file
log "$message"
echo "$message"
fi
[Unit]
Description=Per minute update Cloudflare Dynamic DNS service
[Timer]
OnBootSec=1min
OnUnitActiveSec=1min
AccuracySec=1s
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment