-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathASUSddns.sh
executable file
·162 lines (149 loc) · 4.05 KB
/
ASUSddns.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
160
161
162
#!/bin/sh
asus_request(){
case $mode in
"register")
local path="ddns/register.jsp"
;;
"update")
local path="ddns/update.jsp"
;;
esac
local password=$(calculate_password)
echo $(curl --write-out %{http_code} --silent --output /dev/null --user-agent "ez-update-3.0.11b5 unknown [] (by Angus Mackay)" --basic --user $user:$password "http://ns1.asuscomm.com/$path?hostname=$host&myip=$wanIP")
}
calculate_password(){
local stripped_host=$(strip_dots_colons $host)
local stripped_wanIP=$(strip_dots_colons $wanIP)
echo $(echo -n "$stripped_host$stripped_wanIP" | openssl md5 -hmac "$key" 2>/dev/null | cut -d ' ' -f 2 | tr 'a-z' 'A-Z')
}
get_wan_ip(){
echo $(curl --silent http://api.ipify.org/)
#echo $(ifconfig -a $(nvram get pppoe_ifname) 2>/dev/null | grep 'inet addr' | cut -d ':' -f 2 | cut -d ' ' -f 1)
}
is_dns_updated(){
local dns_resolution=$(nslookup $host ns1.asuscomm.com 2>/dev/null)
# check if wanIP is in nslookup result
for token in $dns_resolution
do
if [ $token = $wanIP ]
then
return 0 # true
fi
done
return 1 # false
}
code_to_string(){
case $mode in
"register")
local log_mode="Registration"
;;
"update")
local log_mode="Update"
;;
esac
case $1 in
200 )
echo "$log_mode success."
;;
203 | 233 )
echo "$log_mode failed."
;;
220 )
echo "$log_mode same domain success."
;;
230 )
echo "$log_mode new domain success."
;;
297 )
echo "Invalid hostname."
;;
298 )
echo "Invalid domain name."
;;
299 )
echo "Invalid IP format."
;;
401 )
echo "Authentication failure."
;;
407 )
echo "Proxy authentication Required."
;;
* )
echo "Unknown result code. ($1)"
;;
esac
}
strip_dots_colons(){
echo $(echo "$1" | tr -d .:)
}
log(){
case $output in
"logger")
logger -t "ASUSddns" "$1"
;;
"silent")
;;
*)
echo "$1" >&2
;;
esac
}
main(){
case $mode in
"register")
;;
"update")
if is_dns_updated
then
log "Domain already updated."
return
fi
;;
*)
log "Unknown action! Allowed action: register or update"
return
;;
esac
local return_code=$(asus_request)
local res=$(code_to_string $return_code)
log "$res"
}
usage(){
echo "Usage: mac wps host (register|update) (logger|console|silent)"
echo
echo "mac format: 00:11:22:33:44:55 (asus mac address) [to get it from nvram: nvram get et0macaddr]"
echo "wps format: 12345678 (your wps code) [to get it from nvram: nvram get secret_code]"
echo "host format: testestest (your hostname without .asucomm.com)"
echo
echo "Program output:"
echo "logger --> /var/log/messges"
echo "console --> console"
echo "silent --> mute output"
echo
echo "example to register and update testestest.asuscomm.com:"
echo "$0 00:11:22:33:44:55 12345678 testestest register console"
echo "$0 00:11:22:33:44:55 12345678 testestest update logger"
echo
echo "Launch 'register' the first time to register the new domain with your mac address."
echo "Launch 'update' each 5 minutes (eg: with cron jobs) to keep dns updated."
echo
echo "ASUSddns script by BigNerd95 (https://github.com/BigNerd95/ASUSddns)"
}
if [ $# -eq 5 ]
then
user=$(strip_dots_colons $1)
key=$2
host="$3.asuscomm.com"
mode=$4
output=$5
wanIP=$(get_wan_ip)
if [ -n "${wanIP}" ]
then
main
else
log "No internet connection, cannot check."
fi
else
usage
fi