-
Notifications
You must be signed in to change notification settings - Fork 0
/
upduck.sh
executable file
·117 lines (108 loc) · 2.9 KB
/
upduck.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
113
114
115
116
117
#!/bin/bash
# Simple script to update the IP address that a Duck DNS domain name resolves to.
#
#####
#
# Available environment variables*:
# - $DUCKDNS_DOMAIN: The domain you want to update
# - $DUCKDNS_TOKEN: The token to use to authenticate the request
# - $DUCKDNS_IP: (Optional) The IP address that the provided domain
# should resolve to — Duck DNS will detect and use
# the source IP otherwise
#
# *All other Duck DNS API options are optional; I may implement them later.
#
# NOTE: command line options override environment variables.
#
#####
#
# Error codes:
#
# 1 = invalid arguments
# 2 = failure to find wget on the system
# 3 = no domain provided, by command option or environment variable
# 4 = no token provided, by command option or...
# 5 = Duck DNS returned a 'normal' bad response (check inputs)
# 6 = Duck DNS returned an unexpected error / response
#
#####
#
# wget required; check whether it's available in the environment
if ! [ -x "$(command -v wget)" ]; then
echo "Error: wget required, and not found (or installed?)" >&2
exit 2
fi
# Tell wget to (succinctly) write to stdout
wgetopt="-qO -"
# Strip the script's path, for usage message
name=$(basename "$0")
# Reset arguments, including getopts index
OPTIND=1
dn=""
tkn=""
ip_a=""
while getopts "d:hi:?t:" flag; do
case "$flag" in
d)
dn=${OPTARG}
;;
t)
tkn=${OPTARG}
;;
i)
ip_a=${OPTARG}
;;
h)
printf "Usage: %s [-d domain] [-t token] [-i]\n\t-i\tIP address (detected, if not specified)\n" "$name"
exit 0
;;
\?)
echo "Usage: $name [-d domain] [-t token] [-i IP address]" >&2
exit 1
;;
esac
done
# Check that a domain has been provided
if [[ -z $dn ]]; then
if [[ -z $DUCKDNS_DOMAIN ]]; then
echo "Error: no domain provided. Use -d or \$DUCKDNS_DOMAIN." >&2
exit 3
else
dn=$DUCKDNS_DOMAIN
fi
fi
# Check that a token has been provided
if [[ -z $tkn ]]; then
if [[ -z $DUCKDNS_TOKEN ]]; then
echo "Error: no Duck DNS token provided. Use -t or \$DUCKDNS_TOKEN." >&2
exit 4
else
tkn=$DUCKDNS_TOKEN
fi
fi
# Check whether an IP address has been provided
if [[ -z $ip_a ]]; then
if [[ -n $DUCKDNS_IP ]]; then
ip_a=$DUCKDNS_IP
fi
fi
# Include an IP address in the API call, if available
if [[ -z $ip_a ]]; then
# shellcheck disable=SC2086
resp=$(wget $wgetopt "https://www.duckdns.org/update?domains=$dn&token=$tkn")
else
# shellcheck disable=SC2086
resp=$(wget $wgetopt "https://www.duckdns.org/update?domains=$dn&token=$tkn&ip=$ip_a")
fi
# Parse the response from Duck DNS
if [ "$resp" == "OK" ] && [ -n "$ip_a" ]; then
echo "Success! Domain $dn now resolves to $ip_a."
elif [ "$resp" == "OK" ]; then
echo "Success! Domain $dn is up-to-date."
elif [ "$resp" == "KO" ]; then
echo "Error: update failed :( Try a different domain or token." >&2
exit 5
else
echo "Error: Duck DNS response: $resp." >&2
exit 6
fi