-
Notifications
You must be signed in to change notification settings - Fork 10
/
dyndns.sh
98 lines (85 loc) · 2.53 KB
/
dyndns.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
#!/bin/bash
# Default variable values
# I need to detect IP change
DETECTIP=${DETECTIP:-1}
# Wait for 10 min before each detection
INTERVAL=${INTERVAL:-10}
while true; do
if [ -z "$SERVICE" ]; then
echo "No service was set. Use -e SERVICE=noip|dyndns|duckdns|google|freedns|ovh"
exit 30
fi
if [ -z "$USER" ]; then
echo "No user was set. Use -e USER=username"
exit 10
fi
if [ -z "$PASSWORD" ]; then
echo "No password was set. Use -e PASSWORD=password"
exit 20
fi
if [ -z "$HOSTNAME" ]; then
echo "No host name. Use -e HOSTNAME=host.example.com"
exit 30
fi
if [ -n "$DETECTIP" ]; then
IP=$(wget -qO- "http://myexternalip.com/raw")
if [ -n "$UPDATEIPV6" ]; then
IPV6=$(wget -q --output-document - http://checkipv6.dyndns.com/ | grep -o "[0-9a-f\:]\{8,\}")
fi
fi
if [ -n "$DETECTIP" ] && [ -z $IP ]; then
RESULT="Could not detect external IP."
fi
if [[ $INTERVAL != [0-9]* ]]; then
echo "Interval is not an integer."
exit 35
fi
AUTHTOKEN=0
case "$SERVICE" in
noip)
SERVICEURL="dynupdate.no-ip.com/nic/update?hostname=${HOSTNAME}&myip=${IP}"
URL="https://$USER:$PASSWORD@$SERVICEURL"
;;
dyndns)
SERVICEURL="members.dyndns.org/v3/update?hostname=${HOSTNAME}&myip=${IP}"
;;
duckdns)
AUTHTOKEN=1
SERVICEURL="www.duckdns.org/update?domains=${HOSTNAME}&token=${PASSWORD}&ip=${IP}"
SERVICEURL_V6="www.duckdns.org/update?domains=${HOSTNAME}&token=${PASSWORD}&ip=${IP}&ipv6=${IPV6}"
;;
google)
SERVICEURL="domains.google.com/nic/update?hostname=${HOSTNAME}&myip=${IP}"
;;
freedns)
SERVICEURL="freedns.afraid.org/nic/update?hostname=${HOSTNAME}&myip=${IP}"
;;
ovh)
SERVICEURL="www.ovh.com/nic/update?system=dyndns&hostname=${HOSTNAME}&myip=${IP}"
;;
esac
USERAGENT="User-Agent: no-ip shell script/1.0 mail@mail.com"
AUTHHEADER="Authorization: Basic $(echo -n "$USER:$PASSWORD" | base64)"
[ -z "$URL" ] && URL="https://$SERVICEURL"
if [ -n "$UPDATEIPV6" ]; then
if [ -n "$SERVICEURL_V6" ]; then
URL="https://$SERVICEURL_V6"
else
URL="${URL}&myipv6=$IPV6"
fi
fi
if [ $AUTHTOKEN -eq 0 ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] curl -H \"$USERAGENT\" -H \"$AUTHHEADER\" $URL"
RESULT=$(curl -sS -H "$USERAGENT" -H "$AUTHHEADER" $URL)
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] curl $URL"
RESULT=$(curl -sS $URL)
fi
echo $RESULT
if [ $INTERVAL -eq 0 ]; then
break
else
sleep "${INTERVAL}m"
fi
done
exit 0