Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reboot the modem via expect script.

41 views
Skip to first unread message

Hongyi Zhao

unread,
Jan 11, 2021, 3:17:21 AM1/11/21
to
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.

Regards,
HY

Uwe Klein

unread,
Jan 11, 2021, 4:08:39 AM1/11/21
to
Am 11.01.21 um 09:17 schrieb Hongyi Zhao:
The reboot commmand results in the modem closing the connection. "EOF"
condition. ( and the model is done with the command. )

BUT
You expect script will "just sit there" till doomsday
... or till you detect that EOF condition.
(my guess is you could expect ( with timeout ) "anything" from that
spawn id

Uwe

Hongyi Zhao

unread,
Jan 11, 2021, 4:19:47 AM1/11/21
to
I really can't figure out "anything" apart from the "EOF" condition.
Could you please give me some more notes/examples?
>
> Uwe

heinrichmartin

unread,
Jan 11, 2021, 8:55:50 AM1/11/21
to
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"
}

Hongyi Zhao

unread,
Jan 11, 2021, 10:06:20 AM1/11/21
to
On Monday, January 11, 2021 at 9:55:50 PM UTC+8, heinrichmartin wrote:
> 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

Why you use ';#' as the marks for the starting point of the comments on the above lines? I mean, just using '#' is enough.


> > 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)"

What are the values for the variables referred here: expect_out and string?

> } timeout {
> puts stderr "The modem failed to reboot: timeout"

Why the timeout is not referred to as $timeout?

> }

Rich

unread,
Jan 11, 2021, 11:36:14 AM1/11/21
to
Hongyi Zhao <hongy...@gmail.com> wrote:
> On Monday, January 11, 2021 at 9:55:50 PM UTC+8, heinrichmartin wrote:
>> expect eof ;# wait up to $::timeout (default 10) sec that the modem
>
> Why you use ';#' as the marks for the starting point of the comments
> on the above lines? I mean, just using '#' is enough.

No, it is not enough. Tcl is not Bash. The # for comments is only
recognized where a command name would exist, not anywhere on the line.

So to put a comment on the same line with a command, you first have to
tell the parser that the current command is complete (;) and then tell
the parser, in the location where a command name would exist, that a
comment begins (#).

heinrichmartin

unread,
Jan 11, 2021, 4:43:39 PM1/11/21
to
On the Tcl level, understand that expect_out is an array (that is a key-value-store in Tcl). and "1,string" is the index/key.
Also, $timeout would be replaced by the value of a variable timeout (if it exists - and an error occurs otherwise) before the command ever sees that argument.

For the understanding of expect, read its man page. Both answers are under the command "expect".

HTH
0 new messages