we now track blockip and only add when needed and only delete entries we created.
[toast/tdyndns.git] / bin / client / tdyndns_client
1 #!/bin/sh
2
3 # save as /usr/local/bin/tdyndns
4
5 set -u
6
7 # debug?
8
9 if [ $# -gt 0 ] ; then
10         DOIT="echo "
11         set -x
12 else
13         DOIT=""
14 fi
15
16 # functions
17
18 die() {
19         echo "$1" >&2
20         exit 1
21 }
22
23 warn() {
24         echo "$1" >&2
25 }
26
27 # variables
28
29 USERNAME=sue
30 PASSWORD=hygCithOrs5
31 HOSTNAME=$(hostname)
32 ZONE=dyn.example.com
33 FQDN=$HOSTNAME.$USERNAME.$ZONE
34 NS=ns.example.com
35 URL="http://dyndns.example.com/nic/update?hostname=$FQDN&myip="
36
37 if [ -z "$HOSTNAME" ] ; then
38         die "No hostname found."
39 fi
40
41 # data
42
43 # alternative: -4 icanhazip.com, -6 icanhazip.com or ipv{4,6}.icanhazip.com
44 NEWIPV4=$(wget -q -O- http://ipv4.wtfismyip.com/text)
45 NEWIPV6=$(wget -q -O- http://ipv6.wtfismyip.com/text)
46
47 if command -v dig >/dev/null ; then
48         OLDIPV4=$(dig -t a    +short $FQDN @$NS)
49         OLDIPV6=$(dig -t aaaa +short $FQDN @$NS)
50 fi
51 OLDIPV4=${OLDIPV4:-""}
52 OLDIPV6=${OLDIPV6:-""}
53
54 # actions
55
56 # old ipv4 gone
57 if [ -n "$OLDIPV4" -a -z "$NEWIPV4" ] ; then
58         $DOIT wget -q -O/dev/null --user=$USERNAME --password=$PASSWORD "${URL}${OLDIPV4}&offline=yes"
59         logger -t${0##*/} -perr -- "removing $FQDN / $OLDIPV4"
60 fi
61
62 # old ipv6 gone
63 if [ -n "$OLDIPV6" -a -z "$NEWIPV6" ] ; then
64         $DOIT wget -q -O/dev/null --user=$USERNAME --password=$PASSWORD "${URL}${OLDIPV6}&offline=yes"
65         logger -t${0##*/} -perr -- "removing $FQDN / $OLDIPV6"
66 fi
67
68 # new ipv4
69 if [ -n "$NEWIPV4" -a "$NEWIPV4" != "$OLDIPV4" ] ; then
70         $DOIT wget -q -O/dev/null --user=$USERNAME --password=$PASSWORD "${URL}${NEWIPV4}"
71         logger -t${0##*/} -perr -- "updating $FQDN to $NEWIPV4"
72 fi
73
74 # new ipv6
75 if [ -n "$NEWIPV6" -a "$NEWIPV6" != "$OLDIPV6" ] ; then
76         $DOIT wget -q -O/dev/null --user=$USERNAME --password=$PASSWORD "${URL}${NEWIPV6}"
77         logger -t${0##*/} -perr -- "updating $FQDN to $NEWIPV6"
78 fi