-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate-namecheap
executable file
·91 lines (75 loc) · 2.46 KB
/
update-namecheap
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
#!/usr/bin/env bash
################################################################################
# update-namecheap - simple script for updating Namecheap's dynamic DNS service.
#
# Dependencies: curl (should be found almost everywhere)
#
# Version 1
# Matthew Malensek <[email protected]>
################################################################################
unset username password ip
update_url='dynamicdns.park-your-domain.com/update'
print_usage() {
cat <<EOM
Usage: $(basename ${0}) -u username -p password [-i ip_addr] host
Updates the Namecheap dynamic DNS for a given host. Dynamic DNS must be turned
on in the Namecheap settings. Options:
* -u Namecheap dynamic DNS username (domain name) e.g., somewhere.org
* -p dynamic DNS password (long alphanumeric string generated by Namecheap)
* -i ip address (optional). If not provided, the requesting IP is used.
host - the hostname to update, for instance when updating
site.somewhere.org, host will be 'site.' To update the base hostname,
provide '@'. You may also have a '*' record set up that can be changed.
Examples:
$(basename ${0}) -u somewhere.org -p abc123etcetc mysite
$(basename ${0}) -u somewhere.org -p abc123etcetc @
$(basename ${0}) -u somewhere.org -p abc123etcetc -i 127.0.0.1 othersite
EOM
}
read_xml () {
local IFS=\>
read -d \< tag value
}
while getopts "u:p:i:" flag; do
case ${flag} in
u) username=${OPTARG} ;;
p) password=${OPTARG} ;;
i) ip=${OPTARG} ;;
?) print_usage; exit 1 ;;
esac
done
if [[ -z "${username}" || -z "${password}" ]]; then
echo "A username (-u) and password (-p) must be specified."
echo
print_usage
exit 1
fi
shift $(($OPTIND - 1))
host=${1}
if [[ -z ${1} ]]; then
echo "No host specified to update!"
echo
print_usage
exit 1
fi
url="https://${update_url}?host=${host}&domain=${username}&password=${password}"
if [[ -n "${ip}" ]]; then
url="${url}&ip=${ip}"
fi
response=$(curl "${url}" 2> /dev/null)
errors=0
while read_xml; do
if [[ "${tag}" == "ErrCount" ]]; then
errors=${value}
if [[ ${errors} -eq 0 ]]; then
# There were no errors; we are done.
exit 0
fi
continue
fi
if [[ ${errors} -gt 0 && "${tag}" == "Err"* ]]; then
echo 1>&2 "ERROR: ${value}"
fi
done <<< "${response}"
# If execution reaches this point, errors were listed. Set a failed exit code.
exit 1