Last active
March 4, 2022 05:17
-
-
Save xdung24/7e50e55919493178bc66543ca40d370c to your computer and use it in GitHub Desktop.
Change cloudflare dns record for dynamic IP address
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
Script to update dns record for dynamic 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 | |
# Load env.txt | |
if [ -f env.txt ]; then | |
export $(cat env.txt | xargs) | |
else | |
echo "env.txt file not found" | |
exit 0 | |
fi | |
# Read from .env | |
auth_email=${AUTH_EMAIL} | |
auth_key=${DNS_EDIT_KEY} | |
zone_name=${ZONE_NAME} | |
record_name=${RECORD_NAME} | |
# MAYBE CHANGE THESE | |
ip=$(curl -s http://ipv4.icanhazip.com) | |
ip_file="ip.txt" | |
log_file="log.txt" | |
# LOGGER | |
log() { | |
if [ "$1" ]; then | |
echo -e "[$(date)] - $1" >> $log_file | |
fi | |
} | |
# SCRIPT START | |
log "Check Initiated" | |
if [ -f $ip_file ]; then | |
old_ip=$(cat $ip_file) | |
if [ $ip == $old_ip ]; then | |
log "IP has not changed." | |
echo "IP has not changed." | |
exit 0 | |
fi | |
fi | |
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "Authorization: Bearer $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 ) | |
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 $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*') | |
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 "Authorization: Bearer $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}") | |
if [[ $update == *"\"success\":true"* ]]; then | |
message="IP changed to: $ip" | |
echo "$ip" > $ip_file | |
log "$message" | |
echo "$message" | |
else | |
message="API UPDATE FAILED. DUMPING RESULTS:\n$update" | |
log "$message" | |
echo "$message" | |
fi |
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
AUTH_EMAIL=cloudflare_email | |
DNS_EDIT_KEY=cloudflare_zone_dns_edit_key | |
ZONE_NAME=sample.com | |
RECORD_NAME=.sample.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment