-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoip-update
executable file
·134 lines (108 loc) · 2.81 KB
/
noip-update
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# dynamic dns client for noip.com
set -eu
set -o pipefail
log() {
echo >&2 "$@"
}
logrun() {
log "+ $*"
"$@"
}
basename="$(basename "$0")"
slog() {
log "$@"
logger -t "$basename" <<< "$@"
}
usage() {
cat >&2 <<EOM
usage: $(basename "$0") [options] [ADDRESS]
Configuration values are taken from ~/.noiprc.
If ADDRESS is given, change the DDNS record to ADDRESS regardless of what our
public IP address seems to be.
options:
-h, --help Show this message
--force Force an update even if we think none is needed
--frob Twiddle the DDNS record to prevent expiry
EOM
}
force_update=
frob_update=
while [ $# -gt 0 ] && [[ $1 = -* ]]; do
case "$1" in
--force)
force_update=1
shift
;;
--frob)
# --frob implies --force
frob_update=1
force_update=1
shift
;;
-h,--help)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
force_addr=
if [ $# -gt 1 ]; then
usage
exit 1
elif [ $# -eq 1 ]; then
force_addr="$1"
shift
fi
# duplicate stdout/stderr to syslog
#exec 1> >(tee -a /dev/fd/1 | logger -t "$(basename "$0")")
#exec 2> >(tee -a /dev/fd/2 | logger -t "$(basename "$0")")
# add fd 3 for printing stuff with a prefix
exec 3> >(sed 's/^/>>> /')
trap "echo >&2 ERROR" EXIT
slog "Starting up"
NOIP_RCFILE="$HOME/.noiprc"
. "$NOIP_RCFILE"
if [ -n "$frob_update" ]; then
slog "Performing frob update of $NOIP_HOSTNAME"
"$0" --force 192.0.2.1
slog "Proceeding with frob back to normal address"
fi
log "hostname: $NOIP_HOSTNAME"
log "noip.com username: $NOIP_USERNAME"
log "noip.com password: $(sed 's/./*/g' <<< "$NOIP_PASSWORD")"
pub_addr="$(logrun curl -Ssf https://agb.me/ip/ | tee -a /dev/fd/3)"
dns_addr="$(logrun dig +short "$NOIP_HOSTNAME" | tee -a /dev/fd/3)"
log "Public IP address from HTTP: $pub_addr"
log "Public IP address from DNS: $dns_addr"
if [ -n "$force_addr" ]; then
log "Overriding public IP address with '$force_addr'"
pub_addr="$force_addr"
fi
NOIP_HOST="${NOIP_HOST:-dynupdate.no-ip.com}"
NOIP_ENDPOINT="https://$NOIP_HOST/nic/update"
if [ "$pub_addr" != "$dns_addr" ]; then
slog "Updating $NOIP_HOSTNAME to $pub_addr (was $dns_addr)"
update=1
elif [ -n "$force_update" ]; then
slog "Forcing update even though $NOIP_HOSTNAME is already $pub_addr"
update=1
else
update=
fi
if [ -n "$update" ]; then
netrc="machine $NOIP_HOST login $NOIP_USERNAME password $NOIP_PASSWORD"
logrun curl -sSf --netrc-file <(echo "$netrc") \
"$NOIP_ENDPOINT?hostname=$NOIP_HOSTNAME&myip=$pub_addr" >&3
echo >&3
else
slog "No update to $NOIP_HOSTNAME needed"
fi
# close fd 3 to flush output
3>&-
slog "All done"
trap - EXIT