-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowndns.sh
106 lines (96 loc) · 3.03 KB
/
owndns.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
#!/bin/bash
# Logs date and IP
function log() {
# Log date and IP to logfile
echo -e "$(date +%Y-%m-%d) $IP" >> "$LOGFILE"
}
# Updates ip at registrar
function update_ip() {
# Get current IP
IP=$(curl -s -L $GET_IP_FROM)
LAST_IP=0
# Get last IP if logfile exists
if [ -e "$LOGFILE" ]; then
LAST_IP=$(tail -1 $LOGFILE | cut -d' ' -f2)
fi
# Check if the IP is new
if [ "$IP" != "$LAST_IP" ]; then
# We have a new IP address
case $REGISTRAR in
0)
# iwantmyname
# We don't need to provide &myip because it will take the remote, if none is set
RETURN=$(curl -s \
-u "$EMAIL:$PASSWORD" \
"https://iwantmyname.com/basicauth/ddns?hostname=$DOMAIN")
case $RETURN in
"good $IP")
echo "Success"
;;
"badauth.")
echo "Bad auth, check your config.sh"
;;
"")
echo "Curl failed, check your internet"
;;
*)
echo "An unknown error occured"
;;
esac
log
;;
1)
# CloudFlare
RETURN=$(curl -s \
"https://www.cloudflare.com/api_json.html" \
-d "a=rec_edit" \
-d "tkn=$TOKEN" \
-d "id=$REC_ID" \
-d "email=$EMAIL" \
-d "z=$DOMAIN" \
-d "type=A" \
-d "name=$DOMAIN" \
-d "content=$IP" \
-d "service_mode=1" \
-d "ttl=1")
if [[ $RETURN =~ \"result\":\"success\" ]]; then
echo "Success"
elif [[ $RETURN =~ \"err_code\":\"E_UNAUTH\" ]]; then
echo "Bad auth, check your config.sh"
elif [[ $RETURN =~ \"result\":\"error\" ]]; then
echo "An unknown error occured"
elif [ -z $RETURN ]; then
echo "Curl failed, check your internet"
fi
log
;;
*)
echo "Registrar $REGISTRAR not supported!"
;;
esac
else
echo "The IP address $IP is already set up."
fi
}
# Load config
if [ -e "config.sh" ]; then
# Include config.sh
source config.sh
# Check if --force or -f is appended
# If yes, update registrar, otherwise ask first
if [ "$1" == "--force" -o "$1" == "-f" ]; then
update_ip
else
read -p "This may change the IP address for your domain. Are you sure? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
update_ip
fi
fi
else
echo "No config file found."
echo "Run setup-config.sh or rename config-template.sh to config.sh and fill in your data."
fi
# Unset functions
unset log
unset update_ip