rt3x.de NoPaste

dns-update.sh

This is a nopaste service, powered by Flying Paste.

Author: Malte Bublitz
Language/File type: Bash script

Description

DynDNS update script made for the ServerCow DNS update.php API [1], not their full DNS API [2].

If your provider supports a uses a simple GET request based API you might just need to set $DNS_API_URL_TEMPLATE in your config file.

Crontab example:

@reboot         /usr/bin/env /usr/local/sbin/dns-update.sh
*/15 * * * *    /usr/bin/env /usr/local/sbin/dns-update.sh



[1]: https://cp.servercow.de/plugin/support_manager/knowledgebase/view/49/dns-update-durch-synology/7/

Code

  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env zsh
# 
# dns-update.sh
#     Dynamic DNS update script made for ServerCow DNS.
# 
# Requirements:
#  - zsh
#  - curl
#  - iproute2
#  - dig
# 

SCRIPT_NAME="dns-update.sh"
SCRIPT_VERSION="0.20250106.1"
CONFIGFILE="$HOME/.config/dns-update-sh"



################################################################
# From _base.inc.sh
# 

#source ~malte70/bin/_base.inc.sh

# Write to stdout and support escape-codes
_print() {
	echo -en "$@"
}
# Like _print, but add a newline
_println() {
	echo -e "$@"
}
# Write only to a real terminal
_print_term() {
	[[ $TERM != "dump" ]] && _print "$@"
}
# _print_term with a leading newline
_println_term() {
	[[ $TERM != "dump" ]] && _println "$@"
}
# A regular message
message() {
	_print_term $_ANSI_COLOR_GREEN
	_print "[${SCRIPT_NAME}] "
	_print_term $_ANSI_RESET
	_println "$@"
}
# An error message (on stderr)
message_error() {
	(
		_print_term $_ANSI_COLOR_RED
		_print "[${SCRIPT_NAME}] [ERROR] "
		_print_term $_ANSI_RESET
		_println "$@"
	) >&2
}
# A message with additional information (INFO level, on stderr)
message_info() {
	(
		_print_term $_ANSI_COLOR_YELLOW
		_print "[${SCRIPT_NAME}] [INFO]  "
		_print_term $_ANSI_RESET
		_println "$@"
	) >&2
}
version() {
	echo "$SCRIPT_NAME $SCRIPT_VERSION"
}



################################################################
# Be quiet when executed as cronjob
# 

if [[ "$RUN_CONTEXT" == "crontab" ]]; then
	_print_term(){}
	message_info(){}
fi



################################################################
# Command line options
# 

usage() {
	echo "Usage:"
	echo "   $SCRIPT_NAME [OPTIONS]"
	echo 
	echo "Options:"
	echo "  --version -V  Show the version and exit"
	echo "  --help    -h  Show this help and exit"
	echo "  --dry-run -d  Check for changes, but di not modify DNS records"
	echo 
}

DO_DRY_RUN="false"
while [[ $# -gt 0 ]]; do
	if [[ "$1" == "--help" || "$1" == "-h" ]]; then
		version
		echo
		usage
		exit 0

	elif [[ "$1" == "--version" || "$1" == "-V" ]]; then
		version
		exit 0

	elif [[ "$1" == "--dry-run" || "$1" == "-d" ]]; then
		DO_DRY_RUN="true"

	elif [[ "${1:0:1}" == "-" ]]; then
		message_error "Unknown option: \"$1\""
		exit 2
		
	else
		usage >&2
		exit 2
		
	fi
	
	shift
done



################################################################
# Configuration
# 

if [[ ! -f "${CONFIGFILE}" ]]; then
	message_error "Config file ${CONFIGFILE} does not exist\!" >&2
	message_info "Creating a new file using empty default values."
	echo '# 
# ~/.config/dns-update-sh
# 
#     Config file for /usr/local/sbin/dns-update.sh
# 

# ServerCow DNS API user name
DNS_API_USERNAME=""

# ServerCow DNS API password
DNS_API_PASSWORD=""

# healthchecks.io Ping URL
# Dkipped if this is set to an empty string.
#HEALTHCHECKS_PING_URL="https://healthchecks.example.com/ping/XXXXXXXXXXXXXXXXXXXXXX/dns-update"
HEALTHCHECKS_PING_URL=""

# List of domains to update
DNS_DOMAINS=(
	"example.com"
)
' > "${CONFIGFILE}"
	exit 1

else
	source "${CONFIGFILE}"
fi

if [[ -z $DNS_API_URL_TEMPLATE ]]; then
	# Default DNS API URL for ServerCow
	DNS_API_URL_TEMPLATE="https://www.servercow.de/dnsupdate/update.php?username=${DNS_API_USERNAME}&pass=${DNS_API_PASSWORD}&hostname=%DOMAIN%&ipaddr=%IP%"
fi



################################################################
# Helper funtions
# 

get_ipv4() {
	# Get external IPv4 address
	curl -s "http://ipinfo.io/ip"
}

get_ipv6() {
	# Get IPv6 address
	
	if [[ "$1" == "--with-privacy" ]]; then
		# Returns IPv6 with privacy extensions
		ipv6=$(curl -s -6 "https://ifconfig.co")
	else
		# Returns IPv6 WITHOUT privacy extensions
		ipv6=$(ip addr show eth0 | grep inet6\ 2001 | grep -v :: | cut -d" " -f6 | cut -d/ -f1)
	fi
	echo "$ipv6"
}



################################################################
# Configuration
# 

update_ipv4() {
	# Get current external IP
	MY_IP=$(get_ipv4)
	
	if [[ -z $MY_IP ]]; then
		message_error 'Could not get external IP!'
		exit 1
	fi
	if [[ $MY_IP == $(dig +short ${DNS_DOMAINS[1]}) ]]; then
		message_info "IPv4 not changed"
		return 0
	fi
	
	if [[ $MY_IP != $(dig +short ${DNS_DOMAINS[1]}) ]]; then
		DNS_API_URL=${DNS_API_URL_TEMPLATE:gs/%IP%/$MY_IP}
		
		
		num_failed_ipv4=0
		
		for domain in "${DNS_DOMAINS[@]}"; do
			myurl=${DNS_API_URL:gs/%DOMAIN%/$domain/}
			
			message_info "Updating DNS record for $domain"
			if [[ "$DO_DRY_RUN" == "true" ]]; then
				message_info "API call using curl: curl -s \"$myurl\""
			else
				curl -s "$myurl" --output /dev/null
				
				if [[ $? -ne 0 ]]; then
					message_error "curl returned an error for \"$myurl\""
					num_failed_ipv4=$((num_failed_ipv4 + 1))
				fi
			fi
		done
		
		
		if [[ $num_failed_ipv4 -gt 0 ]]; then
			message_info "Note: From a total number of ${#DNS_DOMAIN[@]} API calls $num_failed_ipv4 failed for IPv4."
		fi
		
	fi
}


update_ipv6() {
	# Returns IPv6 with privacy extensions
	#MY_IP6=$(get_ipv6 --with-privacy)
	# Returns IPv6 WITHOUT privacy extensions
	MY_IP6=$(get_ipv6)
	
	if ! [[ -z $MY_IP ]]; then
		if [[ $MY_IP == $(dig +short ${DNS_DOMAINS[1]} AAAA) ]]; then
			message_info "IPv6 not changed"
			return 0
		fi
		
		#DNS_API_URL_TEMPLATE=${DNS_API_URL_TEMPLATE:gs/$MY_IP/$MY_IP6}
		DNS_API_URL=${DNS_API_URL_TEMPLATE:gs/%IP%/$MY_IP6}
		
		
		num_failed_ipv6=0
		
		for domain in "${DNS_DOMAINS[@]}"; do
			myurl=${DNS_API_URL:gs/%DOMAIN%/$domain/}
			
			message_info "Updating DNS record for $domain"
			if [[ "$DO_DRY_RUN" == "true" ]]; then
				message_info "API call using curl: curl -s \"$myurl\""
			else
				curl -s "$myurl" --output /dev/null
				
				if [[ $? -ne 0 ]]; then
					message_error "curl returned an error for \"$myurl\""
					num_failed_ipv6=$((num_failed_ipv6 + 1))
				fi
			fi
		done
		
	fi
}


update_ipv4
update_ipv6

# Calculate failure statistics
num_failed=$((num_failed_ipv4 + num_failed_ipv6))



# Notify healthchecks.io
# See also: https://healthchecks.rt3x.de/docs/signaling_failures/
if [[ -n $HEALTHCHECKS_PING_URL ]]; then
	curl \
		-s \
		"${HEALTHCHECKS_PING_URL}/${num_failed}" \
		--output /dev/null
fi