upload pod file
[hband-tools.git] / routerscript / epc3925
blob162dd7acc596f278b85f495cfae406a502440b55
1 #!/bin/bash
3 usage="Usage: $0 -I <router-ip> -U <username> -P <password> [command]"
4 declare -A prm
5 cachefile=
6 goform=
7 SessionID=
8 endfilter=
10 get_sessionid()
12 cachefile="/var/cache/sessid_epc3925_${RouterUser}@${RouterIP}"
13 if [ -s "$cachefile" ]
14 then
15 SessionID=`cat "$cachefile"`
17 if [ -z "$SessionID" ]
18 then
19 [ -t 2 ] && echo "Logging in $RouterUser@$RouterIP ..." >&2
20 SessionID=`curl -sSiL http://$RouterIP/goform/Docsis_system -d username_login="$RouterUser" -d password_login="$RouterPass" -d LanguageSelect=en -d Language_Submit=0 -d login="Log In" | grep -E "^Set-Cookie: SessionID=" | cut -d= -f2 | tr -dc "[a-zA-Z0-9]"`
21 if [ -z "$SessionID" ]
22 then
23 echo "Login error." >&2
24 exit 2
25 else
26 true >"$cachefile"
27 chmod 0600 "$cachefile" && \
28 echo -n "$SessionID" >"$cachefile"
32 invalid_sesscache()
34 if [ -n "$cachefile" -a -s "$cachefile" ]
35 then
36 true >"$cachefile"
39 dispatch_epc()
41 get_sessionid
42 local query=$1
43 shift
44 local cnt=0
46 while [ $cnt -lt 2 ]
48 local output=`curl -sS "$@" "http://$RouterIP/$query" -H "Cookie: SessionID=$SessionID; Lang=en"`
49 let cnt++
50 # Check if we got the login page (or whatever this is)
51 if echo "$output" | grep -q 'window.location.href = "Docsis_system.asp"'
52 then
53 invalid_sesscache
54 else
55 echo "$output"
56 return 0
58 done
59 return 1
61 is_integer()
63 [ "$1" -ge 0 ] 2>/dev/null
67 while [ -n "$1" ]; do
68 case "$1" in
69 -U) shift
70 RouterUser=$1
72 -P) shift
73 RouterPass=$1
75 -I) shift
76 RouterIP=$1
78 -*) echo "$usage" 1>&2
79 exit 1
81 --) shift
82 break
84 *) break
86 esac
87 shift
88 done
91 if [ -z "$RouterIP" ]
92 then
93 echo "$usage" 1>&2
94 exit 1
100 case "${1,,}" in
101 psk)
102 shift
103 [ -z "$1" ] && psk=`pwgen 9 1` || psk=$1
104 if [ ${#psk} -lt 8 -o ${#psk} -gt 63 ]; then
105 echo "PSK must be longer than 7 and shorter than 64 chars." 1>&2
106 exit 1
108 echo "PSK=$psk"
110 # wpa_cli set_network 0 psk "\"$psk\""
112 #prm[UserId]=
113 #prm[Password]=
114 #prm[PasswordReEnter]=
115 #prm[setup_wifi_enable]=enable
116 #prm[h_setup_wifi_enable]=enable
117 #prm[ssid]="$essid"
119 # wep psk psk2 psk2_mixed wpa_radius wpa2_radius enterprise_mixed disabled
120 #prm[security_mode]=psk2
121 prm[wifi0_security_mode]=psk2
122 prm[h_wifi0_security_mode]=psk2
124 # aes tkip+aes
125 #prm[wpa_enc]=tkip+aes
126 prm[wifi0_wpa_Enc]=tkip+aes
127 prm[h_wifi0_wpa_Enc]=tkip+aes
129 #prm[wpa_psk_key]="$psk"
130 prm[wifi0_wpaPsk_key]="$psk"
131 #prm[UnhiddenPresharedKey]=0x01
132 prm[ws_UnhiddenPresharedKey]=0x01
134 prm[wifi0_radius_ip_1]=0
135 prm[wifi0_radius_ip_2]=0
136 prm[wifi0_radius_ip_3]=0
137 prm[wifi0_radius_ip_4]=0
138 prm[wifi0_wpa_lifetime]=3600
139 prm[wifi0_keysize]=64
140 prm[wifi0_tx_key]=1
142 prm[wl_wds_setting]=disable
143 prm[save]="Save Settings"
145 goform=WSecurity
148 macfilter)
149 get_sessionid
150 eval `dispatch_epc WMACFilter.asp | perl -ne '/(wl_mac\d+).*?="([^"]+)/ and print "prm[$1]=$2\n";'`
152 shift
153 cmd=${1,,}
154 shift
155 mac=${1^^}
157 if [ "$cmd" = list ]; then
158 for mac in "${prm[@]}"; do
159 if [ "$mac" != 00:00:00:00:00:00 ]; then
160 echo "$mac"
162 done
164 elif [ "$cmd" = add ]; then
165 for pname in "${!prm[@]}"; do
166 if [ "${prm[$pname]}" = 00:00:00:00:00:00 ]; then
167 prm[$pname]=$mac
168 break
170 done
171 goform=WMACFilter
173 elif [ "$cmd" = del ]; then
174 for pname in "${!prm[@]}"; do
175 if [ "${prm[$pname]^^}" = "$mac" ]; then
176 prm[$pname]=00:00:00:00:00:00
178 done
179 goform=WMACFilter
182 prm[wl0_macfilter]=enable
183 prm[wl0_macmode]=allow
184 prm[h_wl0_macfilter]=enable
185 prm[h_wl0_macmode]=allow
186 prm[save]="Save Settings"
189 dhcp)
190 shift
191 cmd=${1,,}
193 if [ "$cmd" = list ]; then
194 get_sessionid
196 { dispatch_epc DHCPReservation.asp -i
197 dispatch_epc FixedCPEIpAssignment.asp -i
198 } | \
199 perl -e '
200 $read1 = $read2 = 0;
201 while(<>) {
202 /vbdhcpdivcename/ and $read1 = 1;
203 /^<html/i and $read1 = 0;
204 if($read1) {
205 /^<TD>(.*)<\/TD>\s*$/ and push @DHCP, $1;
207 if($read2) {
208 for(/>(.*?)(?:<|\s*$)/g) {
209 /(\S+)\s+\S+\s+(\S+).*?(\S+)\s*$/;
210 %{$ARP{$2}} = (mac=>$1, state=>$3);
212 $read2 = 0;
214 /SELECT .*DhcpRsvList/i and $read2 = 1;
217 $cols = 4;
218 $col = 0;
219 @colhdr = qw/port mac ip state/;
220 %node = ();
221 for(@DHCP) {
222 $node{$colhdr[$col]} = $_;
223 $col++;
224 if($col == $cols) {
225 push @Node, {%node};
226 %node = ();
227 $col = 0
231 for(keys %ARP) {
232 %n = %{$ARP{$_}};
233 push @Node, { mac=>$n{mac}, ip=>$_, state=>$n{state} };
236 $col = 0;
237 for my $hdr (@colhdr) {
238 for my $node (@Node) {
239 $len = length $node->{$hdr};
240 $clen{$hdr} = $len if $clen{$hdr} <= $len;
244 $fmt = join("\t", map {"%-".$clen{$_}."s"} @colhdr)."\n";
246 for my $node (sort { $a->{ip} cmp $b->{ip} } @Node) {
247 printf $fmt, map { $node->{$_} } @colhdr;
253 portforward)
254 _proc() {
255 # <INPUT type="text" class="num" maxlength="5" size="5" name="PortForwardPortGlobalStart11" title="Port Forwarding 11 External Start Port" value="8767" />&nbsp;
256 # <script language="javascript" type="text/javascript">dw(vs_to);</script>
257 #</TD>
258 #<TD headers="external ex_end_port">
259 # <INPUT type="text" class="num" maxlength="5" size="5" name="PortForwardPortGlobalEnd11" title="Port Forwarding 11 External End Port" value="8767" />
260 #</TD>
261 #<TD nowrap="nowrap" headers="internal in_ip_address">
262 # <input type=text name=PortForwardAddressLocal11IPX title="Port Forwarding 11 Internal IP Address" size=15 maxlength=15 value=192.168.32.11>
263 #</TD>
264 #<TD nowrap="nowrap" headers="internal in_start_port">
265 # <INPUT type="text" class="num" maxlength="5" size="5" name="PortForwardPortLocalStart11" title="Port Forwarding 11 Internal Start Port" value="8767" />&nbsp;
266 # <script language="javascript" type="text/javascript">dw(vs_to);</script>
267 #</TD>
268 #<TD headers="internal in_end_port">
269 # <INPUT type="text" class="num" maxlength="5" size="5" name="PortForwardPortLocalEnd11" title="Port Forwarding 11 Internal End Port" value="8767" />
270 perl -e '
271 ($mode, $cmd, $gstart, $gend, $lstart, $lend, $dst, $proto_num) = split /\s+/, "'"$*"'";
272 %proto = qw/3 udp 4 tcp 254 any/;
273 while(<>)
275 s/\s*$//;
276 /PortForwardPort(Global|Local)(Start|End)(\d+)".*?(?:value)="(\d+)/ and $pf{$3}->{$1}->{$2} = $4;
277 /PortForwardAddressLocal(\d+)IPX.*?(?:value)=([\d\.:]+)/ and $pf{$1}->{"dst"} = $2;
278 /PortForwardEnable(\d+).*?(checked)/ and $pf{$1}->{"enabled"} = 1;
279 ($prev.$_) =~ /PortForwardProtocol(\d+).*?(=(3|4|254) selected)/ and do{ $pf{$1}->{"proto"} = $proto{$3}; $pf{$1}->{"proto_num"} = $3 };
280 $prev = $_;
282 %pf = map { $_=>$pf{$_}; } grep { $pf{$_}->{"dst"} ne "0.0.0.0" } keys %pf;
283 for(sort { $a<=>$b } keys %pf) {
284 $n = $_;
285 $_ = $pf{$_};
286 if($mode eq "bash") {
287 if($cmd eq "del" and
288 $gstart eq $_->{"Global"}->{"Start"} and $gend eq $_->{"Global"}->{"End"} and
289 $lstart eq $_->{"Local"}->{"Start"} and $lend eq $_->{"Local"}->{"End"} and
290 $dst eq $_->{"dst"} and $proto_num eq $_->{"proto_num"}) {
291 printf "prm[PortForwardPortGlobalStart%d]=0\n", $n;
292 printf "prm[PortForwardPortGlobalEnd%d]=0\n", $n;
293 printf "prm[PortForwardPortLocalStart%d]=0\n", $n;
294 printf "prm[PortForwardPortLocalEnd%d]=0\n", $n;
295 printf "prm[PortForwardAddressLocal%dIPX]=0.0.0.0\n", $n;
296 printf "prm[PortForwardProtocol%d]=254\n", $n;
298 else {
299 printf "prm[PortForwardPortGlobalStart%d]=%s\n", $n, $_->{"Global"}->{"Start"};
300 printf "prm[PortForwardPortGlobalEnd%d]=%s\n", $n, $_->{"Global"}->{"End"};
301 printf "prm[PortForwardPortLocalStart%d]=%s\n", $n, $_->{"Local"}->{"Start"};
302 printf "prm[PortForwardPortLocalEnd%d]=%s\n", $n, $_->{"Local"}->{"End"};
303 printf "prm[PortForwardAddressLocal%dIPX]=%s\n", $n, $_->{"dst"};
304 printf "prm[PortForwardProtocol%d]=%s\n", $n, $_->{"proto_num"};
305 printf "prm[PortForwardEnable%d]=0x01\n", $n if $_->{"enabled"};
308 else {
309 $e = $_->{"enabled"} ? "" : "# ";
310 use Data::Dumper;
311 if($_->{"Global"}->{"Start"} == $_->{"Global"}->{"End"}) {
312 printf "%-6s 0.0.0.0:%-11s %15s:%s\n", $e.$_->{"proto"}, $_->{"Global"}->{"Start"}, $_->{"dst"}, $_->{"Local"}->{"Start"};
314 else {
315 printf "%-6s 0.0.0.0:%-11s %15s:%s\n", $e.$_->{"proto"}, $_->{"Global"}->{"Start"}."-".$_->{"Global"}->{"End"}, $_->{"dst"}, $_->{"Local"}->{"Start"}."-".$_->{"Local"}->{"End"};
319 if($mode eq "bash") {
320 for $n (1..30) {
321 if(!defined $pf{$n}) {
322 print "free=$n\n";
323 last;
330 shift
331 cmd=$1
333 case "$cmd" in
334 list)
335 dispatch_epc AppGaming.asp | _proc list
337 new|del)
338 shift
339 gstart=$1
340 shift
341 if is_integer "$1"; then
342 if [ $# -gt 3 ]; then
343 gend=$1
344 lstart=$2
345 lend=$3
346 shift 3
347 else
348 gend=$gstart
349 lstart=$1
350 lend=$1
351 shift
353 else
354 gend=$gstart
355 lstart=$gstart
356 lend=$gstart
358 dst=$1
359 [ -z "$2" ] && proto=any || proto=$2
361 if ! is_integer "$gstart" || ! is_integer "$gend" || ! is_integer "$lstart" || ! is_integer "$lend" || [ -z "$dst" ]; then
362 echo "Parameters:"
363 echo " portforward new <port> <local ip> [<protocol>]" 1>&2
364 echo " portforward new <global port> <local port> <local ip> [<protocol>]" 1>&2
365 echo " portforward new <global portrange start> <global portrange end> <local portrange start> <local portrange end> <local ip> [<protocol>]" 1>&2
366 echo " portforward del <port> <local ip> <protocol>" 1>&2
367 exit 1
370 declare -A proto_num
371 proto_num[udp]=3
372 proto_num[tcp]=4
373 proto_num[any]=254
375 if [ -z "${proto_num[$proto]}" ]; then
376 echo "Protocols: udp, tcp, any"
377 exit 1
380 get_sessionid
381 eval `dispatch_epc AppGaming.asp | _proc bash "$cmd" "$gstart" "$gend" "$lstart" "$lend" "$dst" "${proto_num[$proto]}"`
383 if [ "$cmd" = new ]; then
384 prm[PortForwardPortGlobalStart$free]=$gstart
385 prm[PortForwardPortGlobalEnd$free]=$gend
386 prm[PortForwardPortLocalStart$free]=$lstart
387 prm[PortForwardPortLocalEnd$free]=$lend
388 prm[PortForwardAddressLocal${free}IPX]=$dst
389 prm[PortForwardProtocol$free]=${proto_num[$proto]}
390 prm[PortForwardEnable$free]=0x01
393 goform=AppGaming
395 esac
398 reboot)
399 prm[mtenRestore]="Device Restart"
400 prm[devicerestart]=1
401 goform=Devicerestart
402 endfilter=http_status
405 next-gw)
406 dispatch_epc Status.asp | grep 'dw(vb_gateway)' | sed -e 's@^.*<b>\(.*\)</b>.*@\1@'
410 echo -n "Commands:
411 psk [<passphrase>]
412 macfilter [list | add <MAC> | del <MAC>]
413 dhcp [list]
414 portforward [list | new | del]
415 reboot
416 next-gw
417 " 1>&2
418 exit 1
420 esac
424 if [ -n "$goform" ]; then
425 declare -a params
426 for pname in "${!prm[@]}"
428 pval=${prm[$pname]}
429 pval=${pval//+/%2B}
430 params+=(-d "$pname=$pval")
431 #echo "$pname=$pval" 1>&2
432 done
434 dispatch_epc "goform/$goform" -i -H "Referer: http://$RouterIP/$goform.asp" "${params[@]}" | \
435 case "$endfilter" in
436 http_status)
437 read http_ver http_status
438 echo "$http_status"
443 esac