2 ################################################
3 # SOHO router commandline configuration script #
5 # 3.6.1 Build 071010 Rel.33028n #
6 # TL-WR543G v2 08140201 #
7 ################################################
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")
21 echo "$self: [command] [-u user] [--] [host]
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
28 stat trafic statistics
29 info status information
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 [*]
42 --dhcp-pool-first [*] --dhcp-pool-last [*]
44 --dhcp-dns-1 --dhcp-dns-2
45 takes minute between 1 and 2880
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"
58 dhclients|hosts|survey|reserv|log|reboot|version|stat|backup|dhcp|setup
)
67 info-lan
) subinfo
=lan
;;
68 info-wireless|info-wlan
) subinfo
=wlan
;;
69 info-wan
) subinfo
=wan
;;
80 --dhcp-pool-first) shift
82 --dhcp-pool-last) shift
84 --dhcp-lease-time) shift
102 [ -z "$mode" ] && help
103 [ -z "$host" -a -n "$1" ] && host="$1"
106 ### passive commands ###
108 curl
-sS -u "$user" "http://$host/userRpm/StatusRpm.htm" | \
110 perl
-ne '$_ =~ /var statusPara = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
111 sed 's/"\([^"]*\)"[, ]*/\1\n/gi' #| \
114 curl
-sS -u "$user" "http://$host/userRpm/StatusRpm.htm" | \
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
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
137 if [ ${sp[1]} -eq 1 ]; then
142 echo "Status: ${wanStatusStringArray[$wsi]}"
144 echo "MAC Address: ${wp[row+1]}"
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]}"
156 if [ "$subinfo" = wlan
-o "$subinfo" = all
]; then
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]}"
168 curl
-Ss -u "$user" "http://$host/userRpm/AssignedIpAddrListRpm.htm" | \
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++}'
175 echo "# Generated /etc/hosts file"
176 echo "# fetched from DHCP association database"
177 echo "# on $tplink router at $host"
180 curl
-sS -u "$user" "http://$host/userRpm/AssignedIpAddrListRpm.htm" | \
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}}'
190 while [ ${items:-$itemsPerPage} -ge $itemsPerPage ]; do
191 curl
-sS -u "$user" "http://$host/userRpm/FixMapCfgRpm.htm?Page=$p" | \
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}'
204 curl
-sS -u "$user" "http://$host/userRpm/SiteSurveyRpm.htm" | \
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++}' | \
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" ]
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`"
221 epochBoot
=$
((10#$epochNow - 10#$uptime))
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"
231 echo "$self: $host: Probably a wrong password. (${http[@]})" >&2
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
239 curl
-s -u "$user" "http://$host/userRpm/SystemStatisticRpm.htm" | \
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
248 backupfile
="config.bin"
249 curl
-sS -u "$user" "http://$host/userRpm/$backupfile" > "$backupfile"
252 if [ -r "$restorefile" ]
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./'
261 echo "$self: $restorefile: not readable" >&2
265 curl
-sSi -u "$user" "http://$host/userRpm/LanDhcpServerRpm.html" | \
267 perl
-ne '$_ =~ /var DHCPPara = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
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 ###
291 curl
-sSi -u "$user" "http://$host/userRpm/SysRebootRpm.htm?Reboot=Reboot" | \
292 awk '{if($2==200){print "Reboot..."}else{$1=""; print}; exit}'
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" | \
302 perl
-ne '$_ =~ /var DHCPPara = new Array\((("[^"]*"|[^;\)])*)\);/ and print $1' | \
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
309 elif [ x
"$dhcpserver" = xoff
]; then
313 dhcp_server
=$dhcp_enable
315 if [ x
"${dhcp_ip_range[1]}" = x
]; then
316 dhcp_ip_range
[1]=$dhcp_pool_first
320 if [ x
"${dhcp_ip_range[2]}" = x
]; then
321 dhcp_ip_range
[2]=$dhcp_pool_last
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
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."