summaryrefslogtreecommitdiff
path: root/update-ddns.in
blob: b8542b8ce35878b77f35df0fc766e04ad04b7055 (plain)
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
#!/bin/bash

set -e

stty &> /dev/null || \
  sleep $[$RANDOM%60]

if ! ipCmd="$(which ip 2> /dev/null)"
then
  for p1 in / /usr/
  do
    for p2 in bin/ sbin/
    do
      ipCmd="${p1}${p2}ip"
      [ -x "${ipCmd}" ] && break
    done
    [ -x "${ipCmd}" ] && break
  done
fi

while read -r ipVer authToken dev dnsName
do

  if [[ "${ipVer}" = "#"* ]] || [ -z "${ipVer}" ]
  then
    continue
  fi

  if [ ${ipVer} == "4" ]
  then
    dnsVer="A"
    inetVer="inet"
    syncPre=""
  elif [ ${ipVer} == "6" ] 
  then
    dnsVer="AAAA"
    inetVer="inet6"
    syncPre="v6."
  else
    >&2 echo 'Error: First column in "#ETCDIR#/update-ddns.conf" needs to be "4" or "6"!'
    exit 1
  fi

  if [ -z "${dnsName}" ]
  then
    dnsIps="$(
      curl -${ipVer} "${dev}" 2> /dev/null
    )"
    ips="1"
    unset updateIp
  else
    dnsIps="$(
      dig "${dnsName}" ${dnsVer} | \
        grep -v '^\s*\(;\|$\)' | \
        grep "\s${dnsVer}\s" | \
        awk '{print $5}' | \
        sort -u
    )"

    ips="$(
      "${ipCmd}" addr show dev "${dev}" | \
        grep "^\s*${inetVer}\s.*\sscope\sglobal\s" | \
        awk '{print $2}' | \
        cut -d/ -f1 | \
        sort -u
    )"

    if [ $(echo "${ips}" | wc -l) -eq 1 ]
    then
      updateIp='?address='"${ips}"
    else
      unset updateIp
    fi
  fi

  if [ -z "$(
    (
      echo "${dnsIps}"
      echo "${ips}"
    ) | \
      sort | \
      uniq -d
  )" ]
  then
    curl -sS "https://${syncPre}sync.afraid.org/u/${authToken}/${updateIp}" 2> /dev/null
  fi

done < #ETCDIR#/update-ddns.conf