~mikini/hometools

hometools/cron_duckdns -rwxr-xr-x 1.6 KiB
158a6504 — Mikkel Kirkgaard Nielsen Exit on network error & indicate date of rate used a month ago
                                                                                
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
#! /bin/bash
# Update a duckdns.org DDNS entry (expansion of http://www.duckdns.org/install.jsp#linux-cron)
# See also http://www.duckdns.org/spec.jsp

# Source secrets and config file
test -e $0_secrets && . $0_secrets
test -e $0_config  && . $0_config

if [ -z "$DOMAIN" -o -z "$TOKEN" ]; then
  echo -e duckdns.org credentials not fully supplied, see files $(realpath $0)_{secrets,config} |tee --append $LOG
  exit 1
fi       
		     
echo >>$LOG
date -Is >>$LOG
wget "https://duckdns.org/update?domains=${DOMAIN}&token=${TOKEN}&verbose" -O- -q |tr \\n " " >>$LOG
echo >>$LOG

set -o pipefail
while [ -z $IP_DUCK ]; do
  IP_DUCK=$(dig +noall +answer A ${DOMAIN}.duckdns.org|cut -f5)
  DIGS=$?
  if [ ! $DIGS -eq 0 ]; then
    echo -e "Error digging A record of ${DOMAIN}.duckdns.org: status $DIGS\nOutput: $IP_DUCK" |tee --append $LOG
    exit 2
  fi
done

for d in $A_CHECK_DOMAINS; do
    DUCK_OUT=$(dig +noall +answer A $d ${NS:+@$NS})
    IP=$(echo "$DUCK_OUT"|cut -f6)
    DIGS=$?
    if [ $DIGS -eq 0 ];then
      if [ ! "$IP" == "$IP_DUCK" ]; then
        echo "ERROR A record on $d: ${IP} (A on $d) != $IP_DUCK (A on ${DOMAIN}.duckdns.org)"|tee --append $LOG |mail $MAIL_DEST -s "[A record error] $d"
      else
          OK="OK A record on $d: ${IP} (A on $d) == $IP_DUCK (A on ${DOMAIN}.duckdns.org)"
          echo $OK >>$LOG 
          if [ ! -z $MAIL_OK ]; then
            echo $OK |mail $MAIL_DEST -s "[A record ok] $d"
          fi
      fi
    else
      echo -e "Error digging A record of $d: status $DIGS\nOutput: $DUCK_OUT" |tee --append $LOG
      exit 3
    fi
done