add some boilerplate scripts
[hband-tools.git] / routerscript / linksys-WAP54Gv3_1
blob2de9850f8d2029b4a5427b91dfeda715c33651d7
1 #!/bin/bash
3 # Linksys Wireless Router configuration commandline tool
4 # tested on
5 # - WAP54G v3.1 with default firmware v3.04
8 self="${0##*/}"
9 iwlist=
11 get_page() {
12 curl -sSi -u "$user:$pass" http://$linksys_host/$1
13 #wget -q -O - --save-headers --http-user=$user --http-password=$pass http://$linksys_host/$1
15 get_ips() {
16 get_page Setup_static.asp | perl -e '
17 if(<> =~ /^.*\s401\b/) {
18 exit 4;
20 while(<>) { if($_ =~ /^f.GTK/) {
21 my $var = $_;
22 $var =~ s/^([^_]+_){2}([^\.]*)\..*$/$2/;
23 chomp $var;
24 $_ =~ s/^/\$/;
25 # print $$var;
26 $$var = eval $_;
28 $\ = "\n";
29 print $devicename;
30 print join ".", ($ipaddr1, $ipaddr2, $ipaddr3, $ipaddr4);
31 print join ".", ($netmask1, $netmask2, $netmask3, $netmask4);
32 print join ".", ($gateway1, $gateway2, $gateway3, $gateway4);
33 exit 0;'
35 set_ips() {
36 query="action=GTK_Submit"
37 for vars in GTK_wl_devicename:dn GTK_wan_ipaddr:ipw GTK_wan_netmask:nm GTK_wan_gateway:gw GTK_lan_ipaddr:ipl GTK_lan_dhcp:dhcp; do
38 name=${vars%%:*}
39 env=${vars##*:}
40 value=${!env}
41 if [ -n "$value" ]; then
42 query="$query&$name=$value"
44 done
46 curl -sSi -u "$user:$pass" -F "$query" http://$linksys_host/sysconf.cgi |\
47 sed -ne 1p
49 set_global() {
50 curl -sSi -u "$user:$pass" -F "$1" http://$linksys_host/sysconf.cgi |\
51 sed -ne 1p
53 set_admin() {
54 if [ "$npw" = "%" ]; then
55 read -s -e -p "New administration password: " pw1
56 echo
57 read -s -e -p "Retype: " pw2
58 echo
59 else
60 pw1="$npw"
61 pw2="$npw"
63 if [ ."$pw1" != ."$pw2" ]; then
64 return 2
66 if [ ${#pw1} -lt 4 -o ${#pw1} -gt 16 ]; then
67 return 3
70 query="action=Apply&page=password"
71 for vars in http_username:un http_passwd:pw1 confirm_http_passwd:pw1; do
72 name=${vars%%:*}
73 env=${vars##*:}
74 value=${!env}
75 if [ -n "$value" ]; then
76 query="$query&$name=$value"
78 done
80 set_global "$query"
82 set_ap_client() {
83 perl -e 'exit 6 if("'"${mac_connect_to//\"/}"'" !~ /^([0-9A-F]{2}:){5}([0-9A-F]{2})$/i)'
84 query="ap_mode=1&ap_client_mac=$mac_connect_to"
85 set_global "$query"
87 error() {
88 case $1 in
89 0) true;;
90 2) echo "$self: passwords mismatch" >&2;;
91 3) echo "$self: invalid length, ]4;16[" >&2;;
92 4) echo "$self: unauthorized, wrong password?" >&2;;
93 6) echo "$self: specify a MAC address, A1:2B:C3:4D:E5:6F" >&2;;
94 *) echo "$self: unhandled error" >&2;;
95 esac
96 return $1
99 while [ -n "$1" ]; do
100 case "$1" in
101 -h|--help)
102 echo "$self [-option parameter] ... [host]
103 Options:
104 -u username to authenticate
105 -p password to authenticate
106 -p % prompt for password
107 --wan-ip dot-separated IPv4 decimal octets
108 --gateway
109 --netmask
110 --lan-ip
111 --dhcp 0|1
112 -dhcp alias for --dhcp 0
113 +dhcp alias for --dhcp 1
114 --ssid valid SSID string
115 --device valid name for the device
116 -P set new admin password
117 -P % prompt for new admin password
118 --list lists of APs
119 --conn switch to AP client mode and connect to MAC
120 host hostname or ip address of Linksys router
121 Tested on: WAP54G"
122 exit;;
123 -u) shift; user=$1;;
124 -p) shift; pass=$1;;
125 -P) shift; npw=$1;;
126 --wan-ip) shift; ipw=$1;;
127 --gateway) shift; gw=$1;;
128 --netmask) shift; nm=$1;;
129 --lan-ip) shift; ipl=$1;;
130 --dhcp) shift; dhcp=$1;;
131 -dhcp) dhcp=0;;
132 +dhcp) dhcp=1;;
133 --device) shift; dn=$1;;
134 --ssid) shift; ssid=$1;;
135 --list) iwlist=1;;
136 --conn) shift; mac_connect_to=$1;;
137 --) shift; break;;
138 -*) false;;
139 *) break;;
140 esac
141 shift
142 done
144 linksys_host=$1
145 shift
147 if [ ."$pass" = ."%" ]; then
148 read -s -e -p "Router password: " pass
149 echo
152 if [ -z "$linksys_host" ]; then
153 echo "$self: specify a host" >&2
154 exit 1
155 elif [ -n "$npw" ]; then
156 set_admin
157 error $?
158 elif [ -n "$ipw$gw$nm$ipl$dhcp$dn" ]; then
159 # if any variable set
160 set_ips
161 elif [ $iwlist ]; then
162 get_page site_results.asp | awk '/<!-- SiteSurvay Table start/,/<!-- SiteSurvay Table End/' | perl -e '
163 if(<> =~ /^.*\s401\b/) {
164 exit 4;
166 while(<>) {
167 if($_ =~ /<input type="(hidden|radio)"/) {
168 $_ =~ /name="([^\d"]+)(\d+)?"\s*value="([^"]*)"/;
169 $var = $1;
170 $num = $2+0;
171 $val = $3;
172 $var =~ s/ap_//;
173 $aps[$num]{$var} = $val;
174 #printf "%8s[%1s] = %s\n", ($var,$num,$val);
175 } elsif($_ =~ />(\d{1,3})%</) {
176 $aps[$num]{signal} = $1;
179 $count = $aps[0]{cnt};
180 printf "Networks: %2d\n------------\n", ($count);
181 for (@aps) {
182 %ap = %$_;
183 printf "%s ch:%2d %32s %3d%% ", ($ap{ap}, $ap{channel}, $ap{ssid}, $ap{signal});
184 print "[WEP]" if($ap{wep}); print "[WPA]" if($ap{wpa});
185 print "\n";
187 exit 0;'
188 error $?
189 elif [ -n "$mac_connect_to" ]; then
190 set_ap_client
191 error $?
192 else
193 get_ips
194 error $?