-
Notifications
You must be signed in to change notification settings - Fork 0
/
duckdns
executable file
·83 lines (72 loc) · 2.83 KB
/
duckdns
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
#!/usr/bin/env bash
# duckdns - Update duckdns.org DDNS service
# Usage: duckdns [-d] # -d adds a timestamp in the log
# Domain and token variables need to be set below.
# Example crontab lines (replace /PATH/TO by the actual path to the script):
# 0 0 * * * /PATH/TO/duckdns -d
# */15 * * * * /PATH/TO/duckdns
# Required: wget grep
# Get WAN_IP by using dig on a TXT/A/AAAA record:
# dig -4 +short TXT o-o.myaddr.l.google.com @ns1.google.com # Or -6, With ""
# dig -4 +short myip.opendns.com @resolver1.opendns.com # Or AAAA instead of -4, Blocks recursive resolvers
# dig +short TXT whoami.cloudflare CH @1.1.1.1 # No IPv6, With "", Through CHaosnet
# dig +short whoami.akamai.net @ns1-1.akamaitech.net # No IPv6
# dig -4 ip @dns.toys # Or -6
# Show your (local) recursive resolver through dig:
# dig -4 +short TXT o-o.myaddr.l.google.com # With ""
# dig -4 +short whoami.akamai.net
# Using dig on dns.toys to get all kinds of services
# Get: help, time, weather, unit, base & currency conversions, number to words, definitions, CIDR range, dice roll, coin toss, random numbers:
# dig help @dns.toys
# dig bangkok.time @dns.toys
# dig bangkok.weather @dns.toys
# dig 24oz-ml.unit @dns.toys # See dig unit @dns.toys
# dig 755oct-bin.base @dns.toys # dec/hex/bin/oct
# dig 100THB-EUR.fx @dns.toys
# dig 987654321.words @dns.toys
# dig dandruff.dict @dns.toys
# dig 2001:db8::/108.cidr @dns.toys
# dig 3d20/2.dice @dns.toys
# dig 3.coin @dns.toys
# dig 30-200.rand @dns.toys
# duckdns.org subdomain & token, can be set in DUCKDNS_DOMAIN & DUCKDNS_TOKEN
domain=''
token=''
Help(){
echo "duckdns - Update duckdns.org DDNS service"
echo "Usage: duckdns [-d|--date | -h|--help]"
echo " -d/--date: add a timestamp in the log"
echo " -h/--help: display this help text and values of domain and token"
[[ $domain && $token ]] && echo "Domain: $domain, token: $token" && return
echo "Both variables 'domain' and 'token' need to be set in the source"
echo "or in environment variables 'DUCKDNS_DOMAIN' and 'DUCKDNS_TOKEN'"
exit 1
}
duckdir="$HOME"
log="$duckdir/duckdns.log"
msg="$duckdir/duckdns.msg"
# Values set in the source take precedence over the environment variables
: ${domain:=$DUCKDNS_DOMAIN}
: ${token:=$DUCKDNS_TOKEN}
[[ -f "$log" ]] || echo -n "Log duckdns" >"$log"
[[ -f "$msg" ]] || touch "$msg"
ip= res=
url="https://www.duckdns.org/update?domains=$domain&token=$token&verbose=true"
Long(){
res=$(wget -qO- $url)
ip=$(grep -o '[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*' <<<$res)
! grep -q "$ip" <<<$(tail -1 "$msg") &&
echo $res >>"$msg" && echo -ne "\n $ip" >>"$log"
}
[[ $1 = -h || $1 = --help ]] && Help
[[ $1 = -d || $1 = --date ]] && printf '\n%(%Y-%m-%d)T' >>"$log"
Long
if [[ ${res:0:2} = OK ]]
then
echo -n '~' >>"$log"
else
echo -n 'X' >>"$log"
Long
[[ $res && ! ${res:0:2} = OK ]] && echo -e "\n#"$res >>"$log"
fi
exit 0