Last active
January 5, 2022 20:04
-
-
Save DavidEredics/84b5373dcf07ed4f8202e0b7d5e4561f to your computer and use it in GitHub Desktop.
Bash script to update Cloudflare DNS records to the current ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
api_token="" #Cloudflare API Token | |
zone_identifier="" #Zone ID | |
record_name="" #the dns records name (e.g. example.com) | |
ttl=1 #Time to live, in seconds (60-86400 or 1 for automatic) | |
proxied=true #whether the record should proxied by Cloudflare (true|false) | |
ip4=$(dig @resolver1.opendns.com A myip.opendns.com +short -4) #get current ipv4 | |
ip6=$(dig @resolver1.opendns.com AAAA myip.opendns.com +short -6) #get current ipv6 | |
log_file="/var/log/update_ip.log" | |
log() { | |
if [ "$1" ]; then | |
echo -e "[$(date)] - $1" >> $log_file | |
fi | |
} | |
update_ip4() { | |
#check if ip is valid ipv4 address | |
ipv4_regex='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' | |
if [[ $ip4 =~ $ipv4_regex ]]; then | |
A_data=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=A" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json") | |
#get the ip of the dns record | |
A_ip=$(echo $A_data | grep -Po '(?<="content":")[^"]*') | |
#update if A record different | |
if [[ $ip4 != $A_ip ]]; then | |
A_identifier=$(echo $A_data | grep -Po '(?<="id":")[^"]*') | |
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$A_identifier" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip4\",\"ttl\":$ttl,\"proxied\":$proxied}") | |
if [[ $update == *"\"success\":false"* ]]; then | |
message="Update failed:\n$update" | |
log "$message" | |
echo -e "$message" | |
exit 1 | |
else | |
message="IPv4 changed to: $ip4" | |
log "$message" | |
echo "$message" | |
fi | |
else | |
if [ -t 1 ]; then | |
echo $ip4 | |
fi | |
fi | |
else | |
message="Can not get current ipv4 address" | |
log "$message" | |
echo "$message" | |
fi | |
} | |
update_ip6() { | |
#check if ip is valid ipv4 address | |
ipv6_regex='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$' | |
if [[ $ip6 =~ $ipv6_regex ]]; then | |
AAAA_data=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=AAAA" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json") | |
#get the ip of the dns record | |
AAAA_ip=$(echo $AAAA_data | grep -Po '(?<="content":")[^"]*') | |
#update if AAAA record different | |
if [[ $ip6 != $AAAA_ip ]]; then | |
AAAA_identifier=$(echo $AAAA_data | grep -Po '(?<="id":")[^"]*') | |
update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$AAAA_identifier" -H "Authorization: Bearer $api_token" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"name\":\"$record_name\",\"content\":\"$ip6\",\"ttl\":$ttl,\"proxied\":$proxied}") | |
if [[ $update6 == *"\"success\":false"* ]]; then | |
message="Update failed:\n$update6" | |
log "$message" | |
echo -e "$message" | |
exit 1 | |
else | |
message="IPv6 changed to: $ip6" | |
log "$message" | |
echo "$message" | |
fi | |
else | |
if [ -t 1 ]; then | |
echo $ip6 | |
fi | |
fi | |
else | |
message="Can not get current ipv6 address" | |
log "$message" | |
echo "$message" | |
fi | |
} | |
case $1 in | |
4) update_ip4 ;; | |
6) update_ip6 ;; | |
*) | |
update_ip4 | |
update_ip6 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment