· 6 years ago · Nov 16, 2019, 10:01 PM
1#!/bin/sh
2
3# interface going down
4if [ "${0#*ip-down.d/}" = "${0##*/}" ]; then
5 exec killall -w "${0##*/}"
6fi
7
8[ "$PPP_IFACE" = 'pppoe-data' ] || exec logger -p daemon.info -t dyndns "Not applying DNS to iface '$PPP_IFACE'"
9
10read domain </proc/sys/kernel/hostname
11
12domain=$domain.example.com
13email=
14key=
15zone=
16api=https://api.cloudflare.com/client/v4/zones/$zone/dns_records
17
18auth="-H X-Auth-Email:$email -H X-Auth-Key:$key"
19
20updatedns() {
21 ret=0
22 if ! id=$(curl -s $auth $api |jq -er ".result[] | select(.name == \"$domain\") | .id"); then
23 return 1
24 fi
25 uri="$api/$id"
26 json=/tmp/dyndns-$$.json
27
28 curl -sS -XPUT $uri $auth \
29 -H 'Content-Type: application/json' \
30 -d "{\"type\":\"A\", \"name\":\"$domain\", \"content\":\"$IPLOCAL\"}" >$json
31 if jq -ec '.success == false' <$json >/dev/null; then
32 logger -p daemon.warning -t dyndns "server returned '$(jq -c .errors <$json)'"
33 ret=1
34 fi
35 rm -f $json
36 return $ret
37}
38
39i=60
40while [ $i -ge 0 ]; do
41 updatedns && exec logger -p daemon.info -t dyndns "DNS for $domain set to $IPLOCAL"
42 sleep 5
43 logger -p daemon.notice -t dyndns "Retry setting DNS for $domain ($i more tries)"
44 i=$((i - 1))
45done &
46
47exit 0