-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cloudflare-CLI-DDNS-Update.sh
112 lines (95 loc) · 2.73 KB
/
Cloudflare-CLI-DDNS-Update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
###############################################################################
# Cloudflare-CLI #
# https://github.com/cvc90/Cloudflare-CLI/ #
# #
# CLI utility that manages Cloudflare services through the Cloudflare API #
# #
# Script file Cloudflare-CLI-DDNS-Update.sh #
# A script to update Dynamic DDNS for domains in your Cloudflare account #
# #
# Usage: ./Cloudflare-CLI-DDNS-Update.sh <domain> #
###############################################################################
conffile=/etc/Cloudflare-CLI-DDNS-Update.conf
[ -n "$1" ] && conffile=$1
set -e
declare -a dyns
. "$conffile"
set +e
tstamp=`date +%s`
latestip=''
lastchange=''
if [ -z "$zone" ]
then
echo "Zone name is not set up." >&2
exit 1
fi
if [ -z "${dyns[0]}" ]
then
echo "Not any subdomain is set up." >&2
exit 1
fi
echo -n "Getting external IP ... " >&2
# put your script here to obtain your external ip:
# see github.com/bAndie91/extip
if [ ! `which extip` ]; then
currentip=`curl icanhazip.com`
else
currentip=`extip -q`
fi
echo "$currentip" >&2
if [ -z "$currentip" ]
then
echo "Could not get external IP." >&2
exit 1
fi
if [ -n "$subdom" ]
then
echo "Getting DDNS log from CloudFlare ..." >&2
declare -A iplog
IFS=$'\n'
history=`cloudflare list records "$zone" | grep -E "^.*?\\.$subdom\\s" | sed -e 's/\./ /' | awk '{print $1,$5}' | sort -n`
if [ -n "$history" ]
then
for str in $history
do
tag=${str%% *}
ip=${str##* }
iplog[$tag]=$ip
latestip=$ip
lastchange=$tag
done
fi
IFS=$'\n\t '
else
echo "Getting last IP from CloudFlare ..." >&2
sub1=${dyns[0]}
latestip=`cloudflare list records "$zone" | sed -ne "s/^$sub1\s\+\S\+\s\+A\s\+\(\S\+\).*/\1/ p"`
fi
if [ -z "$latestip" ]
then
echo "There is no last entry." >&2
else
echo "Last entry: $latestip" >&2
fi
echo "Record current IP: $currentip" >&2
if [ "$latestip" != "$currentip" ]
then
if type Cloudflare-CLI-DDNS-Update-callback >/dev/null 2>&1
then
Cloudflare-CLI-DDNS-Update-callback "$latestip" "$lastchange" "$currentip"
fi
lastchange=$tstamp
if [ -n "$subdom" ]
then
cloudflare add record "$zone" A "$tstamp.$subdom" "$currentip"
fi
else
echo "Last IP is up to date" >&2
fi
for dyn in "${dyns[@]}"
do
# Update dynamic records anyway
cloudflare set record "$dyn.$zone" type A content "$currentip"
cloudflare -q set record "lastchange.$dyn.$zone" type TXT content "${lastchange:-$tstamp}"
done