-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddns-start
94 lines (71 loc) · 2.21 KB
/
ddns-start
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
#!/bin/sh
# Hostker/Zhujike Dynamic DNS updater.
# Configure
# Hostker/Zhujike account Email
EMAIL=${EMAIL:=}
# API token: https://i.hostker.com/account/api
TOKEN=${TOKEN:=}
# One of these: https://i.zhujike.com/dns/showRecord/domain/
# It needs to be pre-existing.
DOMAIN=${DOMAIN:=}
# Subdomain of $DOMAIN, or @ for the root. Note: This script will not work if
# you have multiple records with the same name. We'll create the record if it
# doesn't exist.
HEADER=${HEADER:=}
#Custom TTL
TTL=${TTL:=300}
################################################################################
set -e
fail() {
echo "$0: $@" >&2
[ -x /sbin/ddns_custom_updated ] && /sbin/ddns_custom_updated 0
exit 1
}
JSON_sh="$(dirname "$0")/JSON.sh"
if [ ! -f "$JSON_sh" ]; then
curl --silent -o "$JSON_sh" \
"https://raw.githubusercontent.com/dominictarr/JSON.sh/master/JSON.sh" \
|| fail "couldn't download JSON.sh"
fi
[ -x "$JSON_sh" ] || chmod +x "$JSON_sh"
if [ -z "$1" ]; then
fail "usage: $0 ip-address"
else
IP_ADDRESS="$1"
fi
req() {
curl --fail --silent "$@" || \
fail "invalid request"
}
get_listing() {
REQ="email=$EMAIL&token=$TOKEN&domain=$DOMAIN"
JSON=$(req -d "$REQ" "https://i.hostker.com/api/dnsGetRecords")
ALL=$(echo "$JSON" | "$JSON_sh" -b)
# Fine the line for the domain that include the name we've configure
LINE=$(echo "$ALL" | grep '\["records", [0-9]\+,"header"\]' | \
grep "\"$HEADER\"")
# The index of this record is the 2nd field...
INDEX=$(echo "$LINE" | cut -d, -f2)
# Use the index to find the record's id
ID_LINE=$(echo "$ALL" | grep "\[\"records\",$INDEX,\"id\"\]")
ID=$(echo "$ID_LINE" | cut -f2)
echo "$ID"
}
create_record() {
REQ="email=$EMAIL&token=$TOKEN&domain=$DOMAIN&header=$HEADER&type=A&data=$IP_ADDRESS&ttl=$TTL"
req -d "$REQ" "https://i.hostker.com/api/dnsAddRecord"
}
update_record() {
REQ="email=$EMAIL&token=$TOKEN&id=$1&data=$IP_ADDRESS&ttl=$TTL"
req -d "$REQ" "https://i.hostker.com/api/dnsEditRecord"
}
EXISTING=$(get_listing)
if [ -z "$EXISTING" ]; then
create_record &>/dev/null || \
fail "couldn't create record"
else
update_record "$EXISTING" &>/dev/null || \
fail "couldn't update existing record"
fi
[ -x /sbin/ddns_custom_update ] && /sbin/ddns_custom_update 1
exit 0