-
Notifications
You must be signed in to change notification settings - Fork 6
/
gratisddns.sh
executable file
·160 lines (122 loc) · 3.39 KB
/
gratisddns.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
scriptDir=$( cd "$(dirname "$0")" ; pwd -P )
scriptPath="${scriptDir}/$(basename "$0")"
crondPath="/etc/cron.d/1gratisddns"
function usage {
cat << EndOfUsage
Usage: ./gratisddns.sh [OPTIONS..]
Required options:
-u/--user <user>
-p/--password <ddns password>
-a/--accountdomain <account domain>
-d/--dyndomain <domain name to update>
Optional options:
-t/--detectip
This tells the script to include our external ip as a url parameter. Using this adds a dependency on
the 'dig' command from the 'bind-utils' package. You dont seem to need this.
-i/--ip <ip address>
This tells the script to submit the given ip address as a url parameter. This overrules the -t/--detectip
argument
-s/--schedule
Causes this script to schedule itself for regular runs with the given arguments.
-c/--cancel
Causes this script to unschedule itself
EndOfUsage
}
# Parse command line arguments
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h|--help)
usage
exit 0
;;
-u|--user)
DDNSUSER="$2"
shift # past argument
;;
-p|--password)
PASSWORD="$2"
shift # past argument
;;
-a|--accountdomain)
ACCOUNT_DOMAIN="$2"
shift # past argument
;;
-d|--dyndomain)
DYN_DOMAIN="$2"
shift # past argument
;;
-t|--detectip)
DETECT_IP=YES
shift # past argument
;;
-i|--ip)
EXPLICIT_IP="$2"
shift # past argument
;;
-s|--schedule)
SCHEDULE=YES
shift # past argument
;;
-c|--cancel)
echo "Cancelling gratisddns schedule (if there was one)"
sudo rm "$crondPath"
exit
;;
*)
# unknown option
echo "Unknown option: $key" >&2
usage >&2
exit 1
;;
esac
shift # past argument or value
done
function validateArg {
if [ -z "$1" ]; then
echo "Missing argument: $2" >&2
usage >&2
exit 2
fi
if [ -z "$3" ]; then
echo "$2" = "$1"
fi
}
validateArg "$DDNSUSER" "user"
validateArg "$PASSWORD" "password" "dontprint"
validateArg "$ACCOUNT_DOMAIN" "accountdomain"
validateArg "$DYN_DOMAIN" "dyndomain"
# Done parsing/validating arguments
if [ -n "$SCHEDULE" ]; then
echo "Creating schedule to run this script every three hours and log to /tmp/gratisddns.log"
echo "01 0-23/3 * * * $USER $scriptPath -u $DDNSUSER -p $PASSWORD -a $ACCOUNT_DOMAIN -d $DYN_DOMAIN > /tmp/gratisddns.log 2>&1" | sudo tee "$crondPath" >/dev/null
exit
fi
if [ -n "$EXPLICIT_IP" ]; then
externalIpArg="&i=${EXPLICIT_IP}"
elif [ -n "$DETECT_IP" ]; then
# Verify presence of dig command and provide advice if it is missing
if ! type "dig" &> /dev/null; then
cat << EndOfError >&2
This script requires the command 'dig' when invoked with -t/--detectip. You can probably install it in
one of the following ways:
sudo yum install bind-utils
sudo apt-get install bind-utils
EndOfError
exit 3
fi
echo "Resolving external ip address using opendns.."
externalIp="$(dig +short myip.opendns.com @resolver1.opendns.com)"
if [ $? -ne 0 ]; then
echo "External ip detection failed" >&2
exit 1
fi
echo "Got external ip address $externalIp"
externalIpArg="&i=${externalIp}"
fi
# Time to submit the request
baseUrl="https://ssl.gratisdns.dk/ddns.phtml"
gratisdnsUrl="${baseUrl}?u=${DDNSUSER}&p=${PASSWORD}&d=${ACCOUNT_DOMAIN}&h=${DYN_DOMAIN}${externalIpArg}"
echo "Initiating request to ${baseUrl}.."
curl -sS "$gratisdnsUrl"