#!/usr/bin/expect # Copyright (C) 2023, JF Morreeuw. All rights reserved. # # zyxel.exp v1.0.1 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . proc abort {} { close; exit } proc atcr_reboot {} { send -- "sys atcr reboot\r" expect { ZySH {} } abort } proc resetcm {} { send -- "sys resetcm\r" set timeout 60 expect { ZySH {} timeout abort eof exit } set timeout 10 sleep 60 set counter 0 while { [cellwan_status] != "Up" } { incr counter if { $counter == 24 } { atcr_reboot } else { sleep 10 } } } proc cellwan_status {} { global status global band global rsrp global sinr send -- "cfg cellwan_status get\r" expect { -re {Status:[ \t]*([^ \t]+)[ \t]*} { set status $expect_out(1,string); exp_continue } -re {Band:[ \t]*([^ \n\r\t]*)[ \t]*} { set band $expect_out(1,string); exp_continue } -re {RSRP:[ \t]*([^ \t]+)[ \t]*} { set rsrp $expect_out(1,string); exp_continue } -re {SINR:[ \t]*([^ \t]+)[ \t]*} { set sinr $expect_out(1,string); exp_continue } ZySH {} timeout abort eof exit } return $status } proc ifconfig { timer } { global rxbytes global txbytes send -- "ifconfig\r" expect { -re {wwan0.*RX bytes:(\d+) .*TX bytes:(\d+)} { set rxbytes $expect_out(1,string) set txbytes $expect_out(2,string) exp_continue } ZySH {} timeout abort eof exit } if { $timer == 0 } { set timer [timestamp] send -- "speedtest\r" set timeout -1 expect { ZySH {} eof exit } send -- "ping -c 4 ns0.fdn.org\r" expect { ZySH {} eof exit } set timeout 10 set timer [expr [timestamp]-$timer] if { $timer == 0 } { set timer 1 } } else { sleep $timer } send -- "ifconfig\r" expect { -re {wwan0.*RX bytes:(\d+) .*TX bytes:(\d+)} { set rxbytes [expr ($expect_out(1,string)-$rxbytes)/$timer] set txbytes [expr ($expect_out(2,string)-$txbytes)/$timer] exp_continue } ZySH {} timeout abort eof exit } return $rxbytes } set status "Unknown" set band "Unknown" set rsrp "Unknown" set sinr "Unknown" set rxbytes 0 set txbytes 0 set password [lindex $argv 0] if { "$password" == "" } { set password "mypassword" } spawn ssh admin@ip.ip.ip.ip expect { "*?assword:*" { send -- "$password\r" } timeout abort eof exit } expect { ZySH {} denied abort timeout abort eof exit } if { [cellwan_status] != "Up" } { sleep 30 set counter 0 while { [cellwan_status] != "Up" } { incr counter if { $counter == 24 } { resetcm; break } else { sleep 10 } } } ifconfig 5 if { ($sinr <= 2 && $rxbytes <= 5000) || $rxbytes <= 500 } { ifconfig 0 if { ($sinr <= 2 && $rxbytes <= 2000) || $rxbytes <= 500 } { send -- "cfg cellwan_mapn edit --Index 1\r" expect { ZySH {} timeout abort eof exit } sleep 30 set counter 0 while { [cellwan_status] != "Up" } { incr counter if { $counter == 24 } { resetcm; break } else { sleep 10 } } if { [ifconfig 10] == 0 } { atcr_reboot } } } send_user "\r\n" send_user "===> Cellwan status $status $band dB:$rsrp SNR:$sinr $rxbytes/$txbytes Bytes/s\r\n" close exit