diff --git a/cf-ddns.sh b/cf-ddns.sh index 2ac1792..2980518 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -3,7 +3,7 @@ # Uses curl to be compatible with machines that don't have wget by default # modified by Ross Hosman for use with cloudflare. -# Use $1 to force a certain IP. +# Use parameters to force a certain IP. V4_URL='https://api.cloudflare.com/client/v4' DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #sets the directory of this executable @@ -11,10 +11,9 @@ source $DIR/credentials INTERFACE=${2-"eth0"} #If using local IP, need to specify if not eth0 -IPSOURCE=$(curl -s http://www.google.com/search?q=my+ip) -CURRENT_IPV6=$(echo "$IPSOURCE"|sed -n 's/.*(Client IP address: \([^)]*\)).*$/\1/p') -CURRENT_IPV4=$(echo "$IPSOURCE"|egrep IP.*\:|sed 's/^.*\ \([0-9]*\.[0-9]*\.[0-9]*.[0-9]*\).*/\1/') -CURRENT_IPLOCAL=$(ifconfig $INTERFACE |grep inet\ |cut -d ":" -f 2|cut -d " " -f 1) +CURRENT_IPV4=$(curl https://v4.ident.me/ 2>/dev/null) +CURRENT_IPV6=$(curl https://v6.ident.me/ 2>/dev/null) +CURRENT_IPLOCAL=$(ip address show $INTERFACE | grep "inet " | xargs | cut -d " " -f2 | cut -d "/" -f1) case $1 in 6 ) TYPE="AAAA" @@ -27,34 +26,30 @@ case $1 in WAN_IP=${1-$CURRENT_IPV4} ;; esac -#echo "WAN_IP=$WAN_IP CURRENT_IPV6=$CURRENT_IPV6 CURRENT_IPV4=$CURRENT_IPV4 CURRENT_IPLOCAL=$CURRENT_IPLOCAL cfhost=$cfhost" - +#echo "WAN_IP=$WAN_IP CURRENT_IPV6=$CURRENT_IPV6 CURRENT_IPLOCAL=$CURRENT_IPLOCAL cfhost=$cfhost" function domainid() { -curl -X GET $V4_URL'/zones' \ - -H 'X-Auth-Email: '$email \ - -H 'X-Auth-Key: '$cfkey \ - -H 'Content-Type: application/json' 2>/dev/null |jq "."|\ -jq ".result[]|select(.name==\"$zone\")|.id" -r -} - -function domain_records() { -curl -X GET $V4_URL"/zones/$(domainid)/dns_records" \ - -H 'X-Auth-Email: '$email \ - -H 'X-Auth-Key: '$cfkey \ - -H 'Content-Type: application/json' 2>/dev/null |jq "." + curl -X GET $V4_URL'/zones' \ + -H "Authorization: Bearer $cfkey" \ + -H 'Content-Type: application/json' 2>/dev/null |jq "."|\ + jq ".result[]|select(.name==\"$zone\")|.id" -r } -function record_id () { -domain_records|jq ".result[]|select(.name==\"$1.$zone\")|.id" -r +function record_id() { + curl -X GET $V4_URL"/zones/$(domainid)/dns_records" \ + -H "Authorization: Bearer $cfkey" \ + -H 'Content-Type: application/json' 2>/dev/null |jq ".result[]|select(.name==\"$1.$zone\")|.id" -r } OLD_WAN_IP=$(host -t $TYPE ${cfhost}.${zone}|cut -d " " -f 4) -if [ "$WAN_IP" = "$OLD_WAN_IP" ]; then - echo "IP Unchanged ($WAN_IP = $OLD_WAN_IP)" >/dev/null #commented out with /dev/null becasue of "if" +if [ "$WAN_IP" == "$OLD_WAN_IP" ]; then + echo "IP Unchanged ($WAN_IP = $OLD_WAN_IP)" + exit 0 else - echo "Updating DNS to $WAN_IP" + echo "Updating DNS to $WAN_IP (from $OLD_WAN_IP)" +fi + function generate_post_data(){ cat </dev/null |jq ".success" -r +} + +if [ "$(update_dns_record)" == "true" ]; then + echo "Successfully updated record." +else + echo "There was an error updating the record" fi