-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdate-record
executable file
·60 lines (53 loc) · 1.9 KB
/
update-record
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
#!/bin/sh
exec 3>&1
# get int value for given log level debug=>10, info=>20, error=>40
get_log_level_value(){
level=20
case $1 in
debug)
level=10
;;
info)
level=20
;;
error)
level=40
;;
esac
echo $level
}
# functions used to display logs
log(){
log_level_value=$(get_log_level_value $1)
if [ $log_level_value -ge $current_level_value ]
then
echo "{\"date\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\", \"level\": \"$1\", $levelvalue \"message\": \"$2\"}" 1>&3
fi
}
debug(){ log debug "$1"; }
info(){ log info "$1"; }
error(){ log error "$1"; }
# validate inputs and set defaults
[ -z $HOSTNAME ] && echo You need to set environment variable HOSTNAME && exit
[ -z $IDENTIFIER ] && echo You need to set environment variable IDENTIFIER && exit
[ -z $PASSWORD ] && echo You need to set environment variable PASSWORD && exit
[ -z $LOG_LEVEL ] && LOG_LEVEL=debug
# set current log level, all logs with a lower level are filtered
current_level_value=$(get_log_level_value $LOG_LEVEL)
# retrieve last known (cached) address and current public address
CACHED_IP_ADDRESS=$(cat /dynhost/.cache/ip_address 2>/dev/null)
PUBLIC_IP_ADDRESS=$(dig @resolver4.opendns.com myip.opendns.com +short)
# compare current address to cached one to avoid useless updates
if [ "$CACHED_IP_ADDRESS" != "$PUBLIC_IP_ADDRESS" ]
then
info "Cached address changed from '$CACHED_IP_ADDRESS' to '$PUBLIC_IP_ADDRESS'"
# update DynHost record using OVH API
ovh_response=$(curl -sS "http://www.ovh.com/nic/update?system=dyndns&hostname=${HOSTNAME}&myip=${PUBLIC_IP_ADDRESS}" \
-H "Authorization: Basic $(echo -n $IDENTIFIER:$PASSWORD | base64)")
debug "OVH response: ${ovh_response}"
# cache address for comparison, to avoid useless updates
mkdir -p .cache/
echo $PUBLIC_IP_ADDRESS > /dynhost/.cache/ip_address
else
debug "No changes detected"
fi