forked from acabal/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-linode-dns-record
executable file
·67 lines (55 loc) · 1.78 KB
/
update-linode-dns-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
61
62
63
64
65
66
67
#!/bin/sh
usage(){
fmt <<EOF
DESCRIPTION
Check if the IP address of this host matches the DNS records of a remote host. If it doesn't match, update the DNS records using the Linode API.
USAGE
update-dns-record -h,--host=HOSTNAME -k,--api-key=LINODEAPIKEY -d,--domain-id=LINODEDOMAINID -r,--resource-id=LINODERESOURCEID
EOF
exit
}
die(){ printf "Error: ${1}\n" 1>&2; exit 1; }
require(){ command -v $1 > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" -o "$1" = "-h" ]; then usage; fi fi
#End boilerplate
require "curl" "Try: apt-get install curl"
dynamicHost=""
linodeApiKey=""
domainId=""
resourceId=""
for i in "$@"
do
case "${i}" in
-h=*|--host=*)
dynamicHost="$(echo ${i} | sed 's/[-a-zA-Z0-9]*=//')"
;;
-k=*|--api-key=*)
linodeApiKey="$(echo ${i} | sed 's/[-a-zA-Z0-9]*=//')"
;;
-d=*|--domain-id=*)
domainId="$(echo ${i} | sed 's/[-a-zA-Z0-9]*=//')"
;;
-r=*|--resource-id=*)
resourceId="$(echo ${i} | sed 's/[-a-zA-Z0-9]*=//')"
;;
esac
done
if [ "${dynamicHost}" = "" -o "${linodeApiKey}" = "" -o "${domainId}" = "" -o "${resourceId}" = "" ]; then
usage
fi
#look up the existing dns entry
dnsIp=$(dig +short "${dynamicHost}")
if [ "${dnsIp}" = "" ]; then
die "Could not resolve ${dynamicHost} while looking up DNS records."
fi
#look up our external ip
localIp=$(curl ifconfig.me 2> /dev/null)
if [ $? -ne 0 ]; then
die "Could not resolve ifconfig.me while looking up local IP."
fi
if [ "${dnsIp}" != "${localIp}" ]; then
curl "https://api.linode.com/?api_key=${linodeApiKey}&api_action=domain.resource.update&domainid=${domainId}&resourceid=${resourceId}&target=${localIp}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
die "Could not connect to Linode API."
fi
fi