upload pod file
[hband-tools.git] / routerscript / TpLink-WR543G
blobb048333a0036537fbc83f7289d4cba8fc71d028b
1 #!/bin/bash
2 ################################################
3 # SOHO router commandline configuration script #
4 # tested on: #
5 # 3.6.1 Build 071010 Rel.33028n #
6 # TL-WR543G v2 08140201 #
7 ################################################
9 tplink="TL-WR543G"
10 dhcp_server=
11 dhcp_lease_time=
12 dhcp_gateway=
13 dhcp_domain=
14 self=${0##*/}
15 wlanTypeStringArray=(" " "11Mbps (802.11b)" "54Mbps (802.11g)" "108Mbps (Static)" "108Mbps (Dynamic)")
16 wanStatusStringArray=(" " "Disabled" "Timeout" "Link Down" "Link Up")
17 wanTypeStringArray=(" " "DHCP" "Static IP" "PPPoE" "IEEE802.1X + DHCP" "IEEE802.1X + Static IP" "BigPond Cable" "L2TP" "PPTP")
20 help() {
21 echo "$self: [command] [-u user] [--] [host]
22 Commands:
23 dhclients list hosts that requested DHCP address
24 hosts generate /etc/hosts file
25 survey list of available access points
26 reserv DHCP address reservation
27 log system log
28 stat trafic statistics
29 info status information
30 info-lan
31 info-wan
32 info-wlan
33 version version information about device
34 backup save all configuration settings
35 restore load back saved configuration
36 reboot reboot device software
37 dhcp show DHCP configuration
38 setup set one or more parameters of these:
39 takes no additional argument:
40 --dhcp-on [*] --dhcp-off [*]
41 takes IP address:
42 --dhcp-pool-first [*] --dhcp-pool-last [*]
43 --dhcp-gateway
44 --dhcp-dns-1 --dhcp-dns-2
45 takes minute between 1 and 2880
46 --dhcp-lease-time
47 takes domain name:
48 --dhcp-domain [*]
49 No sanity check.
50 [*] The new config won't take effect until the Router reboots.
51 user username for basic http authenticate
52 host ip or dns address of $tplink device"
53 exit 1
56 while [ -n "$1" ]; do
57 case "$1" in
58 dhclients|hosts|survey|reserv|log|reboot|version|stat|backup|dhcp|setup)
59 mode="$1";;
60 restore)
61 mode="$1"
62 shift
63 restorefile="$1";;
64 info*)
65 mode=info
66 case "$1" in
67 info-lan) subinfo=lan;;
68 info-wireless|info-wlan) subinfo=wlan;;
69 info-wan) subinfo=wan;;
70 *) subinfo=all;;
71 esac;;
72 -h|--help)
73 help;;
74 -u) shift
75 user="$1";;
76 --dhcp-on)
77 dhcp_server=on;;
78 --dhcp-off)
79 dhcp_server=off;;
80 --dhcp-pool-first) shift
81 dhcp_ip_range[1]=$1;;
82 --dhcp-pool-last) shift
83 dhcp_ip_range[2]=$1;;
84 --dhcp-lease-time) shift
85 dhcp_lease_time=$1;;
86 --dhcp-gateway) shift
87 dhcp_gateway=$1;;
88 --dhcp-domain) shift
89 dhcp_domain=$1;;
90 --dhcp-dns-1) shift
91 dhcp_dns[1]=$1;;
92 --dhcp-dns-2) shift
93 dhcp_dns[2]=$1;;
94 --) shift
95 break;;
96 -*) false;;
97 *) host=$1;;
98 esac
99 shift
100 done
102 [ -z "$mode" ] && help
103 [ -z "$host" -a -n "$1" ] && host="$1"
105 case $mode in
106 ### passive commands ###
107 version)
108 curl -sS -u "$user" "http://$host/userRpm/StatusRpm.htm" | \
109 tr -d "\n\r" | \
110 perl -ne '$_ =~ /var statusPara = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
111 sed 's/"\([^"]*\)"[, ]*/\1\n/gi' #| \
113 info)
114 curl -sS -u "$user" "http://$host/userRpm/StatusRpm.htm" | \
115 tr -d "\n\r" | \
116 sed -e 's/var/\n&/g' | sort | \
117 perl -ne '$_ =~ /^var ([wl]{1,2}an|status)Para = new Array\((("[^"]*"|[^;\)])*)\);/ and print $2."\n"' | \
118 perl -ne 'eval "\@list=($_)" and print join("\n",@list)."\n\"\n"' | {
120 while read lp[$i] && [ "${lp[$i]}" != "\"" ]; do i=$((i+1)); done
122 while read sp[$i] && [ "${sp[$i]}" != "\"" ]; do i=$((i+1)); done
124 while read wp[$i] && [ "${wp[$i]}" != "\"" ]; do i=$((i+1)); done
126 while read wlp[$i] && [ "${wlp[$i]}" != "\"" ]; do i=$((i+1)); done
128 if [ "$subinfo" = lan -o "$subinfo" = all ]; then
129 echo "[LAN]"
130 echo "MAC Address: ${lp[0]}"
131 echo "IP Address: ${lp[1]}"
132 echo "Subnet Mask: ${lp[2]}"
134 if [ "$subinfo" = wan -o "$subinfo" = all ]; then
135 for ((i=1; i<=sp[1]; i++)); do
136 row=$[ (i-1)*sp[2] ]
137 if [ ${sp[1]} -eq 1 ]; then
138 echo "[WAN]"
139 else
140 wsi=${wp[$row]}
141 echo "[WAN $i]"
142 echo "Status: ${wanStatusStringArray[$wsi]}"
144 echo "MAC Address: ${wp[row+1]}"
145 wti=${wp[row+3]}
146 echo "IP Address: ${wp[row+2]}"
147 echo "IP Type: ${wanTypeStringArray[$wti]}"
148 echo "Subnet Mask: ${wp[row+4]}"
149 echo "Default Gateway: ${wp[row+7]}"
150 echo "DNS Server(s): ${wp[row+11]}"
151 if [ 0$wti -eq 6 -o 0$wti -eq 7 ]; then
152 echo "Online Time: ${wp[row+12]}"
154 done
156 if [ "$subinfo" = wlan -o "$subinfo" = all ]; then
157 echo "[Wireless]"
158 echo "Wireless Radio: $([ 0${wlp[0]} -eq 0 ] && echo Disabled || echo Enabled)"
159 echo "Name (SSID): ${wlp[1]}"
160 echo "Channel: ${wlp[6]}"
161 echo "Mode: ${wlanTypeStringArray[${wlp[3]}]}"
162 echo "MAC Address: ${wlp[4]}"
166 dhclients)
167 cols=4
168 curl -Ss -u "$user" "http://$host/userRpm/AssignedIpAddrListRpm.htm" | \
169 tr -d "\n\r" | \
170 perl -ne '$_ =~ /var DHCPDynList = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
171 sed 's/"\([^"]*\)"[, ]*/\1\n/gi' | \
172 awk -v cols=$cols '{if(i%cols==cols-1){print $0}else{printf $0"\t"};i++}'
174 hosts)
175 echo "# Generated /etc/hosts file"
176 echo "# fetched from DHCP association database"
177 echo "# on $tplink router at $host"
178 cols=4
180 curl -sS -u "$user" "http://$host/userRpm/AssignedIpAddrListRpm.htm" | \
181 tr -d "\n\r" | \
182 perl -ne '$_ =~ /var DHCPDynList = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
183 sed 's/"\([^"]*\)"[, ]*/\1\n/gi' | \
184 awk -v cols=$cols '{if(i%cols==cols-1){printf "%-15s\t%s\n",a[2],a[0]; i=0; delete a}else{a[i++]=$0}}'
186 reserv)
187 cols=3
189 itemsPerPage=8
190 while [ ${items:-$itemsPerPage} -ge $itemsPerPage ]; do
191 curl -sS -u "$user" "http://$host/userRpm/FixMapCfgRpm.htm?Page=$p" | \
192 tr -d "\n\r" | \
193 perl -ne '$_ =~ /var dhcpList = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
194 sed 's/"\([^"]*\)"[, ]*/\1\n/gi' | \
195 sed 's/[, ]\+/\n/gi' | \
196 awk -v cols=$cols '{if($1=="0"){null++}else{if((NR-1)%cols==cols-1){print $0}else{printf $0"\t"}}}END{exit (NR-null)/3}'
197 items=$?
198 let p++
199 done | \
200 sort -k 2 -n
202 survey)
203 cols=3
204 curl -sS -u "$user" "http://$host/userRpm/SiteSurveyRpm.htm" | \
205 tr -d "\n\r" | \
206 grep -Eio 'var siteList = new Array("[^"]*"|[^;])*;' | \
207 grep -Eio '"[^"]*"' | \
208 sed 's/"\([^"]*\)"/\1/gi' | \
209 awk -v cols=$cols '{if(i%cols==cols-1){print $0}else{printf $0"\t"};i++}' | \
210 sort -k $cols -nr
212 log)
213 result=$(curl -s -D - -u "$user" "http://$host/userRpm/SystemLogRpm.htm")
214 http=($(echo "$result" | sed -ne '1s/[\n\r]*$//p'))
215 result=$(echo "$result" | tr -d "\n\r")
216 if [ "${http[1]}" = "200" ]
217 then
218 set $(echo "$result" | perl -ne '$_ =~ /var logInf = new Array\(("[^"]*"|[^\)]*)\);/; $_=$1; $_ =~ /"Time = ([0-9 :-]+) ([0-9]+)s"/; print "$1 $2\n"') # '
219 epochNow="`date -d "$1 $2" +%s`"
220 uptime="$3"
221 epochBoot=$((10#$epochNow - 10#$uptime))
223 echo "$result" | \
224 perl -ne '$_ =~ /var logList = new Array\(("[^"]*"|[^\)]*)\);/ and print $1' | \
225 sed 's/"\([^"]*\)"/\1\n/g' | \
226 sed 's/^, //; s/:/ /' | \
227 while read epochCur rest; do
228 echo "`date -d@$((10#$epochCur + 10#$epochBoot))` $rest"
229 done
230 else
231 echo "$self: $host: Probably a wrong password. (${http[@]})" >&2
234 stat)
235 printf "%-35s %-25s %-9s\n" "" Total Current >&2
236 printf "%-35s %-13s %-11s %-6s %-6s %-5s %-5s %-5s\n" "" packets bytes packs bytes ICMP UDP SYN >&2
237 # packs byts pack byts icmp udp syn
238 cols=13
239 curl -s -u "$user" "http://$host/userRpm/SystemStatisticRpm.htm" | \
240 tr -d "\n\r" | \
241 perl -ne '$_ =~ /var statList = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
242 sed 's/"\([^"]*\)"[, ]*/\1\n/gi' | \
243 sed 's/[, ]\+/\n/gi' | \
244 awk -v cols=$cols '{if(i%cols==cols-1){printf "%1i %-17s %-15s %13i %11i %6i %6i %2i/%02i %2i/%02i %2i/%02i\n",a[0],a[2],a[1],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12]; i=0; delete a}else{a[i++]=$0}}'
245 # MAC IP pack.byt. pac.byt.icmp udp syn
247 backup)
248 backupfile="config.bin"
249 curl -sS -u "$user" "http://$host/userRpm/$backupfile" > "$backupfile"
251 restore)
252 if [ -r "$restorefile" ]
253 then
254 echo "$self: uploading... ($restorefile) DO NOT UNPLUG!" >&2
255 curl -sS -u "$user" "http://$host/incoming/Config.cfg" -F "filename=@${restorefile};type=application/x-macbinary" >/dev/null #| html2text
256 # FIXME: check if upload was succesful
257 echo "$self: restarting... You may lose the connection. Timeout set to 9 sec." >&2
258 curl -sS -D - --max-time 9 -u "$user" "http://$host/userRpm/ConfUpdateTemp.htm" | head -n1
259 #| grep -E "var\s+reboottime\s*=" | grep -Eo "[0-9]+" | sed 's/^.*$/You can ^C after & usec./'
260 else
261 echo "$self: $restorefile: not readable" >&2
264 dhcp)
265 curl -sSi -u "$user" "http://$host/userRpm/LanDhcpServerRpm.html" | \
266 tr -d "\n\r" | \
267 perl -ne '$_ =~ /var DHCPPara = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
268 sed 's/"//g' | \
269 awk -F, '{
270 dhcp_enable=$1
271 dhcp_pool_first=$2
272 dhcp_pool_last=$3
273 dhcp_lease=$4
274 dhcp_gw=$5
275 dhcp_dom=$6
276 dhcp_dns_1=$7
277 dhcp_dns_2=$8
279 END {
280 printf "DHCP server %s\n",dhcp_enable==1?"enable":"disable"
281 printf "IP range %s-%s\n",dhcp_pool_first,dhcp_pool_last
282 printf "Address lease time %s\n",dhcp_lease
283 printf "Default gateway %s\n",dhcp_gw
284 printf "Default domain %s\n",dhcp_dom
285 printf "Primary DNS %s\n",dhcp_dns_1
286 printf "Secondary DNS %s\n",dhcp_dns_2
289 ### active commands ###
290 reboot)
291 curl -sSi -u "$user" "http://$host/userRpm/SysRebootRpm.htm?Reboot=Reboot" | \
292 awk '{if($2==200){print "Reboot..."}else{$1=""; print}; exit}'
294 setup)
295 mustreboot=0
296 # round 1
297 # setup dns server
298 if [ x"${dhcp_server}${dhcp_ip_range[*]}${dhcp_lease_time}${dhcp_gateway}${dhcp_domain}${dhcp_dns[*]}" != x ]; then
299 # examine current values
300 curl -sSi -u "$user" "http://$host/userRpm/LanDhcpServerRpm.html" | \
301 tr -d "\n\r" | \
302 perl -ne '$_ =~ /var DHCPPara = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
303 sed 's/"//g' | \
305 IFS=, read dhcp_enable dhcp_pool_first dhcp_pool_last dhcp_lease dhcp_gw dhcp_dom dhcp_dns_1 dhcp_dns_2 rest
306 if [ x"$dhcp_server" = xon ]; then
307 dhcp_server=1
308 mustreboot=1
309 elif [ x"$dhcpserver" = xoff ]; then
310 dhcp_server=0
311 mustreboot=1
312 else
313 dhcp_server=$dhcp_enable
315 if [ x"${dhcp_ip_range[1]}" = x ]; then
316 dhcp_ip_range[1]=$dhcp_pool_first
317 else
318 mustreboot=1
320 if [ x"${dhcp_ip_range[2]}" = x ]; then
321 dhcp_ip_range[2]=$dhcp_pool_last
322 else
323 mustreboot=1
325 if [ x"${dhcp_lease_time}" = x ]; then
326 dhcp_lease_time=$dhcp_lease
328 if [ x"${dhcp_gateway}" = x ]; then
329 dhcp_gateway=$dhcp_gw
331 if [ x"${dhcp_domain}" = x ]; then
332 dhcp_domain=$dhcp_dom
333 else
334 mustreboot=1
336 if [ x"${dhcp_dns[1]}" = x ]; then
337 dhcp_dns[1]=$dhcp_dns_1
339 if [ x"${dhcp_dns[2]}" = x ]; then
340 dhcp_dns[2]=$dhcp_dns_2
343 echo "Setup DNS server..." >&2
344 curl -s -u "$user" "http://$host/userRpm/LanDhcpServerRpm.htm?dhcpserver=${dhcp_server}&ip1=${dhcp_ip_range[1]}&ip2=${dhcp_ip_range[2]}&Lease=${dhcp_lease_time}&gateway=${dhcp_gateway}&domain=${dhcp_domain}&dnsserver=${dhcp_dns[1]}&dnsserver2=${dhcp_dns[2]}&Submit=Save" >/dev/null
345 if [ $mustreboot = 1 ]; then
346 echo "The new configure won't take effect until the Router reboot."
351 esac
353 return=$?
354 echo >&2
355 exit $return