Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions cf-ddns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
# 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
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"
Expand All @@ -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 <<EOF
{
Expand All @@ -65,9 +60,16 @@ cat <<EOF
}
EOF
}
curl -X PUT $V4_URL"/zones/$(domainid)/dns_records/$(record_id $cfhost)" \
-H 'X-Auth-Email: '$email \
-H 'X-Auth-Key: '$cfkey \
-H 'Content-Type: application/json' \
--data "$(generate_post_data)"

function update_dns_record () {
curl -X PUT $V4_URL"/zones/$(domainid)/dns_records/$(record_id $cfhost)" \
-H "Authorization: Bearer $cfkey" \
-H 'Content-Type: application/json' \
--data "$(generate_post_data)" 2>/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