summaryrefslogtreecommitdiff
path: root/update-ddns.in
blob: a6dea00c24fe1da115362a3cfec96d20bc9764cf (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
89
90
91
92
93
94
95
96
97
98
99
100
#!/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 updateUrl dev dnsName
do

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

  if [ "${ipVer}" == "4" ]
  then
    dnsVer="A"
    inetVer="inet"
    ipCmdVer='-4'
  elif [ "${ipVer}" == "6" ]
  then
    dnsVer="AAAA"
    inetVer="inet6"
    ipCmdVer='-6'
  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 $1 "${dnsName}" ${dnsVer} | \
        grep -v '^\s*\(;\|$\)' | \
        grep "\s${dnsVer}\s" | \
        awk '{print $5}' | \
        sort -u
    )"

    if [ "${dev}" == "-" ]
    then
      unset dev
    else
      dev="dev ${dev}"
    fi

    ips="$(
      "${ipCmd}" -o "${ipCmdVer}" addr show ${dev} scope global | \
        awk '{print $4}' | \
        cut -d/ -f1 | \
        sort -u
    )"

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

  if [ -z "$(
    (
      echo "${dnsIps}"
      echo "${ips}"
    ) | \
      sort | \
      uniq -d
  )" ]
  then
    curl -${ipVer} -sS "${updateUrl}${updateIp}" 2> /dev/null
  fi

done < #ETCDIR#/update-ddns.conf