#!/bin/bash ########################################################################### ## Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 ## ## Please see: http://www.gnu.org/licenses/gpl.html for legal details, ## ## rights of fair usage, the disclaimer and warranty conditions. ## ########################################################################### ########################################################################### ## CONFIGURATION ## ########################################################################### # Set this to the cloudflare zone name. ZONE='zone.com' # Set this to the A record name. RECORD_NAME='www.zone.com' # Set this to your account e-mail address. EMAIL='support@zone.com' # Set this to your API key. API_KEY='336d5ebc5436534e61d16e63ddfca327' # This is a list of IPs to fail over. The preceedence # is from left to right. The script will attempt the # first address, then the second address, etc... IPS=( 63.143.64.40 24.165.34.50 ) ########################################################################### ## INTERNALS ## ########################################################################### for ip in "${IPS[@]}"; do IP=`ifconfig | grep $ip | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1 }'` if [ -z $IP ]; then continue fi FLARE=$(curl -s https://www.cloudflare.com/api_json.html \ -d 'a=rec_load_all' \ -d "tkn=$API_KEY" \ -d "email=$EMAIL" \ -d "z=$ZONE") RECORD_ID=`echo $FLARE | sed -E "s/.*\"rec_id\":\"([0-9]+?)\",.*,\"zone_name\":\"($ZONE)\",.*,\"type\":\"A\",.*/\1/"` FLARE=$(curl -s https://www.cloudflare.com/api_json.html \ -d 'a=rec_edit' \ -d "tkn=$API_KEY" \ -d "id=$RECORD_ID" \ -d "email=$EMAIL" \ -d "z=$ZONE" \ -d 'type=A' \ -d "name=$RECORD_NAME" \ -d "content=$IP" \ -d 'service_mode=1' \ -d 'ttl=1') RESULT=`echo $FLARE | sed -E "s/.*,\"result\":\"(.*?)\",.*/\1/"` if [ $RESULT == "success" ]; then echo "A record for $RECORD_NAME updated to: $IP" exit 0 fi exit 1 done