From fffab7b5a687408da516b436260c7f7206249bc1 Mon Sep 17 00:00:00 2001 From: Ioannis Karavas Date: Tue, 1 Oct 2019 23:33:08 +0300 Subject: [PATCH 1/6] Replaced Google IP for ident.me , much faster/simpler. Updated local IP to use ip utility instead of deprecated ifconfig. --- cf-ddns.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cf-ddns.sh b/cf-ddns.sh index 2ac1792..832bb0c 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -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/) +CURRENT_IPV6=$(curl https://v6.ident.me/) +CURRENT_IPLOCAL=$(ip address show $INTERFACE | grep "inet " | xargs | cut -d " " -f2 | cut -d "/" -f1) case $1 in 6 ) TYPE="AAAA" @@ -27,8 +26,7 @@ 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' \ @@ -55,6 +53,7 @@ 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" else echo "Updating DNS to $WAN_IP" + function generate_post_data(){ cat < Date: Wed, 2 Oct 2019 00:32:19 +0300 Subject: [PATCH 2/6] Updated authorization for Cloudflare, fixed name selection --- cf-ddns.sh | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/cf-ddns.sh b/cf-ddns.sh index 832bb0c..15c94cc 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -3,16 +3,16 @@ # 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 +INTERFACE=${2-"bridge0"} #If using local IP, need to specify if not eth0 -CURRENT_IPV4=$(curl https://v4.ident.me/) -CURRENT_IPV6=$(curl https://v6.ident.me/) +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 @@ -26,25 +26,22 @@ case $1 in WAN_IP=${1-$CURRENT_IPV4} ;; esac -#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 "Authorization: Bearer $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 \ + curl -X GET $V4_URL"/zones/$(domainid)/dns_records" \ + -H "Authorization: Bearer $cfkey" \ -H 'Content-Type: application/json' 2>/dev/null |jq "." } +# Get the Record (CFHOST) ID function record_id () { -domain_records|jq ".result[]|select(.name==\"$1.$zone\")|.id" -r +domain_records|jq ".result[]|select(.name==\"$cfhost\")|.id" -r } OLD_WAN_IP=$(host -t $TYPE ${cfhost}.${zone}|cut -d " " -f 4) @@ -65,9 +62,11 @@ cat < Date: Wed, 2 Oct 2019 01:06:42 +0300 Subject: [PATCH 3/6] Fixed small bugs --- cf-ddns.sh | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/cf-ddns.sh b/cf-ddns.sh index 15c94cc..8780ba9 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -26,30 +26,35 @@ case $1 in WAN_IP=${1-$CURRENT_IPV4} ;; esac +#echo "WAN_IP=$WAN_IP CURRENT_IPV6=$CURRENT_IPV6 CURRENT_IPLOCAL=$CURRENT_IPLOCAL cfhost=$cfhost" + function domainid() { -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 + 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 domain_records() { curl -X GET $V4_URL"/zones/$(domainid)/dns_records" \ - -H "Authorization: Bearer $cfkey" \ - -H 'Content-Type: application/json' 2>/dev/null |jq "." + -H "Authorization: Bearer $cfkey" \ + -H 'Content-Type: application/json' 2>/dev/null |jq "." } # Get the Record (CFHOST) ID function record_id () { -domain_records|jq ".result[]|select(.name==\"$cfhost\")|.id" -r + domain_records|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 ($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 From d06e26af97d429f5d688e5fbdfb4d340c71a65db Mon Sep 17 00:00:00 2001 From: Ioannis Karavas Date: Wed, 2 Oct 2019 01:07:40 +0300 Subject: [PATCH 4/6] Defaulted to eth0 for interface. --- cf-ddns.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf-ddns.sh b/cf-ddns.sh index 8780ba9..c7c6e81 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -9,7 +9,7 @@ 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-"bridge0"} #If using local IP, need to specify if not eth0 +INTERFACE=${2-"eth0"} #If using local IP, need to specify if not eth0 CURRENT_IPV4=$(curl https://v4.ident.me/ 2>/dev/null) CURRENT_IPV6=$(curl https://v6.ident.me/ 2>/dev/null) From d53f939622782a43215d091c6e1d764b677a6b50 Mon Sep 17 00:00:00 2001 From: Ioannis Karavas Date: Wed, 2 Oct 2019 01:08:33 +0300 Subject: [PATCH 5/6] Removed superfluous function --- cf-ddns.sh | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/cf-ddns.sh b/cf-ddns.sh index c7c6e81..1ace6ab 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -35,16 +35,10 @@ function domainid() { jq ".result[]|select(.name==\"$zone\")|.id" -r } - -function domain_records() { +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 "." -} - -# Get the Record (CFHOST) ID -function record_id () { - domain_records|jq ".result[]|select(.name==\"$1.$zone\")|.id" -r + -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) From 9c7f49c56d657bb3ea0ee58aa9945702e41ce3d4 Mon Sep 17 00:00:00 2001 From: Ioannis Karavas Date: Wed, 2 Oct 2019 01:12:22 +0300 Subject: [PATCH 6/6] Cleaned up naming a bit, removed debugging echos --- cf-ddns.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cf-ddns.sh b/cf-ddns.sh index 1ace6ab..2980518 100755 --- a/cf-ddns.sh +++ b/cf-ddns.sh @@ -47,7 +47,7 @@ if [ "$WAN_IP" == "$OLD_WAN_IP" ]; then echo "IP Unchanged ($WAN_IP = $OLD_WAN_IP)" exit 0 else - echo "Updating DNS ($WAN_IP from $OLD_WAN_IP)" + echo "Updating DNS to $WAN_IP (from $OLD_WAN_IP)" fi function generate_post_data(){ @@ -61,8 +61,6 @@ cat <