On Monday, January 11, 2021 at 10:08:39 AM UTC+1, Uwe Klein wrote:
> Am 11.01.21 um 09:17 schrieb Hongyi Zhao:
> > As noted <
https://superuser.com/questions/758864/trying-to-reboot-adsl-modem-via-expect-script>, I find that for rebooting the modem via expect script, the following command must be used:
> >
> > send "reboot\r"
> > # reboot the modem.
> > expect eof
> >
> > But I still can't figure out why I have to use the "expect eof" as the last command for rebooting the modem. Any hints will be highly appreciated.
My assumption is that they do not want to terminate the session with the modem prematurely, but let the modem close it.
send "reboot\r" ;# assumingly on the modem's console: command it to reboot
expect eof ;# wait up to $::timeout (default 10) sec that the modem closes the console connection
> You expect script will "just sit there" till doomsday
> ... or till you detect that EOF condition.
Not sure if this was (a) your explanation of why to use expect eof or (b) a warning that expect eof could hang if the session with the modem is not closed (e.g. wrong command "reboot" or a serial connection that does not terminate). (b) would have been wrong, because expect has a default timeout.
Anyway, to also act on the result, use code blocks along with the pattern, e.g.
expect -timeout 5 eof {
# success
} -re {error: ([^\r\n]+)} {
# refine the re above to the modem's output!
puts stderr "The modem failed to reboot: $expect_out(1,string)"
} timeout {
puts stderr "The modem failed to reboot: timeout"
}