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

tcl/expect hang

52 views
Skip to first unread message

kbuz...@gmail.com

unread,
May 4, 2007, 2:05:28 PM5/4/07
to
Hello,
I seem to be having a problem where my expect scripts seem to hang
sometimes. If I press 'enter' it will then continue to run the
script. I am not entirely positive but I believe it is related to the
sleep command. It does not happen consistently but when it does
happen it seems to happen in bunches. If it does happen due to sleep
it tends be ones that last a minute or more.
I have seen it happen in a couple of different scripts but the code
mostly looks something like:

while {$a < $b} {

send_user "*************\n"
send "router command \n"
expect {
"certain response" {
# parse expect_out buffer for results
}
timeout {
# quit script
}

}
send_user "parsed info\n"

inrc b
sleep 120
}


thank you in advance,
Matt

Cameron Laird

unread,
May 4, 2007, 5:29:34 PM5/4/07
to
In article <1178301928.2...@e65g2000hsc.googlegroups.com>,
.
.
.
We usually do best with a definite source, symptom and expectation.
I can understand that you don't yet have all those elements. In
their absence, we'll likely just give general remarks in the hope
that we can narrow down the scope of the problem.

I like using Expect with networking equipment such as routers.

I do *not* like a style relies on "sleep 120". I suspect there's a
simpler solution nearby.

Have you tried to use autoexpect?

I bet a few minutes of consultation would result in a much more
robust script.

I assume "inrc b" is a typographical error.

There's a slender chance {send "router command \r"} will serve you
better than {send "router command \n"}.

I suspect there's something more transparent to use than {timeout}.

Bezoar

unread,
May 4, 2007, 7:03:05 PM5/4/07
to

You may be right about the sleep call which ( unless you use the Tclx
extension ) is not a native Tcl
command so the Tcl may be makeing a system call or it might be forking
and execing the sleep program.
You shouild try using "after 120000"


Carl

Bezoar

unread,
May 4, 2007, 7:04:25 PM5/4/07
to
On May 4, 1:05 pm, kbuzz3...@gmail.com wrote:

oops just saw your error you end all your send sequences with \n they
should be \r

so
send "router command\n" should be
send "router command \r"

Carl

kbuz...@gmail.com

unread,
May 6, 2007, 10:18:50 PM5/6/07
to

Hey Folks,
Thanks for the suggestions. The sleep is there because I want to
wait between loops. Depending on the script that I am writing the
wait can be anywhere from two minutes to two hours. (and once in a
while a second or two just because it seems to make things run a bit
smoother with some pauses between the send and expect). I will try
"after" and see if I have any better luck.
Just for the record, I used to end my commands with "\r" but I have
found the "\n" to generally work better. I can't recall anymore the
details but I had some login issues with "\r" in some cases.

thanks again,
Matt

kbuz...@gmail.com

unread,
May 6, 2007, 10:20:09 PM5/6/07
to
On May 4, 7:04 pm, Bezoar <cwjo...@gmail.com> wrote:

Carl,
Just re-reading your post and you wrote "...saw your error...." Are
you saying that you think my problem is directly related to "\n" vs.
"\r"? Or are you just saying that you think that "\r" is a better
choice?

thanks,
Matt

Cameron Laird

unread,
May 7, 2007, 7:37:20 AM5/7/07
to
In article <1178504330....@l77g2000hsb.googlegroups.com>,
.
.
.
I strongly suspect that \n does *not* "generally work better" for
you except in a superficial sense.

I can respect that you've put a lot into your network scripting.
I think Expect is not doing what you think it is. We're not going
to be able to change one small thing and restore the "consistency"
of your application.

An experienced Expect coder can probably clean up your difficulties
in just a few minutes.

It feels as though "better luck" plays a bigger role in this script
than is appropriate. I strongly urge you to some combination of:
A. help with Expect generally;
B. study of <URL: http://wiki.tcl.tk/3173 >;
C. elimination of "sleep", ..., and restriction
of "timeout" to very narrow uses; and
D. study of <URL:
http://groups.google.com/group/comp.lang.tcl/msg/a07a52f9fbb0e840 >.

Cameron Laird

unread,
May 7, 2007, 7:39:30 AM5/7/07
to
In article <1178504409....@u30g2000hsc.googlegroups.com>,
<kbuz...@gmail.com> wrote:
.
.
.

> Just re-reading your post and you wrote "...saw your error...." Are
>you saying that you think my problem is directly related to "\n" vs.
>"\r"? Or are you just saying that you think that "\r" is a better
>choice?
.
.
.
I think your script has multiple issues, some interfering with understanding
of others. While there's a place for both \r and \n in Expect scripts, I
strongly suspect that you'll ultimately find \r more satisfying in the places
you apparently now are using \n.

Bezoar

unread,
May 7, 2007, 10:26:09 AM5/7/07
to

I just say this because usually \r works all the time. If you've used
\n and it works then
thats good enough. I also have an idea of another way to implement
the sleep and that is
to use expect itself to do the sleep. See code below. You can also
put in an eof handler to
detect if the spawned process dies. See if this helps and let us know
if you have solved your
problem.

Carl

#!/opt/usr/bin/tclsh8.5
package require Expect

spawn /bin/sh
set timeout 3
expect {
"\#" {
puts "got prompt"
}
timeout {
puts "timeout"
}
}
# sleep 120 seconds
set timeout 120
set start [clock seconds ]
expect {
timeout {
set end [clock seconds ]
puts "slept for [ expr $end - $start ] sec "
}
}

kbuz...@gmail.com

unread,
May 8, 2007, 11:09:09 AM5/8/07
to

Thank you for all of the suggestions. I have been experimenting with
the "after" command and the early results have been promising (the
timeout idea was also pretty interesting). I will keep trying it out
and hopefully will continue to be a solution to my problem.
Thank you both for taking the time to help.

Matt

0 new messages