/
api
executable file
·90 lines (63 loc) · 2.03 KB
/
api
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
#!/bin/bash
##### NO CONFIGURATION NECESSARY.
MY_PATH="$( cd -P "$( dirname "$0" )" && pwd )"
MY_API_KEY=`cat $MY_PATH/.key 2>/dev/null`
MY_DOMAIN_ID=`cat $MY_PATH/.domain 2>/dev/null`
MY_RESOURCE_ID=`cat $MY_PATH/.resource 2>/dev/null`
LOG_FILE="$MY_PATH/dns.log"
CACHED_IP_FILE="$MY_PATH/.ip"
LINODE_API_URL="https://api.linode.com/?api_key=$MY_API_KEY"
##### IP FUNCTIONS
function get_ip {
local IP=`curl --silent http://checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
[ "$IP" == "" ] && {
echo "[`date`] dyndns lookup failed, checking whatismyipaddress..." >> $LOG_FILE
IP=`curl --silent http://bot.whatismyipaddress.com`
}
[ "$IP" == "" ] && {
echo "[`date`] whatismyipaddress lookup failed, giving up." >> $LOG_FILE
exit 1;
}
echo $IP
}
function get_cached_ip {
cat $CACHED_IP_FILE 2>/dev/null
}
##### LINODE API HELPERS
# Retrieves a list of domains in your linode DNS manager.
function list_domains {
echo $(curl --silent $LINODE_API_URL\&api_action=domain.list) | python -mjson.tool
}
# Retrieves a list of resources (i.e. subdomains) for a particular domain.
# @param Domain ID
function list_resources {
[ $# -ne 1 ] && {
echo "Usage: list_resources domain" 1>&2;
exit 1;
}
echo $(curl --silent $LINODE_API_URL\&api_action=domain.resource.list\&DomainID=$1) | python -mjson.tool
}
# Updates a domain resource.
# @param Domain ID
# @param Resource ID
function update_resource_target {
[ $# -ne 2 ] && {
echo "Usage: update_resource_target domain resource" 1>&2;
exit 1;
}
echo $(curl --silent -g $LINODE_API_URL\&api_action=domain.resource.update\&DomainID=$1\&ResourceID=$2\&TTL_sec=300\&Target=[remote_addr])
}
function update {
IP_OLD=`get_cached_ip`
IP_NEW=`get_ip`
[ "$IP_OLD" != "$IP_NEW" ] && [ "$IP_NEW" != "" ] && {
update_resource_target $MY_DOMAIN_ID $MY_RESOURCE_ID
echo "[`date`] IP changed from $IP_OLD to $IP_NEW" >> $LOG_FILE
}
[ "$IP_NEW" != "" ] && {
echo $IP_NEW > $CACHED_IP_FILE
}
}
##### DO IT
$@
exit 0;