3 usage
="Usage: $0 -I <router-ip> -U <username> -P <password> [command]"
12 cachefile
="/var/cache/sessid_epc3925_${RouterUser}@${RouterIP}"
13 if [ -s "$cachefile" ]
15 SessionID
=`cat "$cachefile"`
17 if [ -z "$SessionID" ]
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" ]
23 echo "Login error." >&2
27 chmod 0600 "$cachefile" && \
28 echo -n "$SessionID" >"$cachefile"
34 if [ -n "$cachefile" -a -s "$cachefile" ]
48 local output
=`curl -sS "$@" "http://$RouterIP/$query" -H "Cookie: SessionID=$SessionID; Lang=en"`
50 # Check if we got the login page (or whatever this is)
51 if echo "$output" |
grep -q 'window.location.href = "Docsis_system.asp"'
63 [ "$1" -ge 0 ] 2>/dev
/null
78 -*) echo "$usage" 1>&2
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
110 # wpa_cli set_network 0 psk "\"$psk\""
114 #prm[PasswordReEnter]=
115 #prm[setup_wifi_enable]=enable
116 #prm[h_setup_wifi_enable]=enable
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
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
142 prm
[wl_wds_setting
]=disable
143 prm
[save
]="Save Settings"
150 eval `dispatch_epc WMACFilter.asp | perl -ne '/(wl_mac\d+).*?="([^"]+)/ and print "prm[$1]=$2\n";'`
157 if [ "$cmd" = list
]; then
158 for mac
in "${prm[@]}"; do
159 if [ "$mac" != 00:00:00:00:00:00 ]; then
164 elif [ "$cmd" = add
]; then
165 for pname
in "${!prm[@]}"; do
166 if [ "${prm[$pname]}" = 00:00:00:00:00:00 ]; then
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
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"
193 if [ "$cmd" = list
]; then
196 { dispatch_epc DHCPReservation.asp
-i
197 dispatch_epc FixedCPEIpAssignment.asp
-i
202 /vbdhcpdivcename/ and $read1 = 1;
203 /^<html/i and $read1 = 0;
205 /^<TD>(.*)<\/TD>\s*$/ and push @DHCP, $1;
208 for(/>(.*?)(?:<|\s*$)/g) {
209 /(\S+)\s+\S+\s+(\S+).*?(\S+)\s*$/;
210 %{$ARP{$2}} = (mac=>$1, state=>$3);
214 /SELECT .*DhcpRsvList/i and $read2 = 1;
219 @colhdr = qw/port mac ip state/;
222 $node{$colhdr[$col]} = $_;
233 push @Node, { mac=>$n{mac}, ip=>$_, state=>$n{state} };
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;
255 # <INPUT type="text" class="num" maxlength="5" size="5" name="PortForwardPortGlobalStart11" title="Port Forwarding 11 External Start Port" value="8767" />
256 # <script language="javascript" type="text/javascript">dw(vs_to);</script>
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" />
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>
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" />
266 # <script language="javascript" type="text/javascript">dw(vs_to);</script>
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" />
271 ($mode, $cmd, $gstart, $gend, $lstart, $lend, $dst, $proto_num) = split /\s+/, "'"$*"'";
272 %proto = qw/3 udp 4 tcp 254 any/;
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 };
282 %pf = map { $_=>$pf{$_}; } grep { $pf{$_}->{"dst"} ne "0.0.0.0" } keys %pf;
283 for(sort { $a<=>$b } keys %pf) {
286 if($mode eq "bash") {
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;
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"};
309 $e = $_->{"enabled"} ? "" : "# ";
311 if($_->{"Global"}->{"Start"} == $_->{"Global"}->{"End"}) {
312 printf "%-6s 0.0.0.0:%-11s %15s:%s\n", $e.$_->{"proto"}, $_->{"Global"}->{"Start"}, $_->{"dst"}, $_->{"Local"}->{"Start"};
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") {
321 if(!defined $pf{$n}) {
335 dispatch_epc AppGaming.asp | _proc list
341 if is_integer
"$1"; then
342 if [ $# -gt 3 ]; then
359 [ -z "$2" ] && proto
=any || proto
=$2
361 if ! is_integer
"$gstart" ||
! is_integer
"$gend" ||
! is_integer
"$lstart" ||
! is_integer
"$lend" ||
[ -z "$dst" ]; then
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
375 if [ -z "${proto_num[$proto]}" ]; then
376 echo "Protocols: udp, tcp, any"
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
399 prm
[mtenRestore
]="Device Restart"
402 endfilter
=http_status
406 dispatch_epc Status.asp |
grep 'dw(vb_gateway)' |
sed -e 's@^.*<b>\(.*\)</b>.*@\1@'
412 macfilter [list | add <MAC> | del <MAC>]
414 portforward [list | new | del]
424 if [ -n "$goform" ]; then
426 for pname
in "${!prm[@]}"
430 params
+=(-d "$pname=$pval")
431 #echo "$pname=$pval" 1>&2
434 dispatch_epc
"goform/$goform" -i -H "Referer: http://$RouterIP/$goform.asp" "${params[@]}" | \
437 read http_ver http_status