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