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

send/expect in a loop causes CPU to choke

52 views
Skip to first unread message

Sharad

unread,
Apr 4, 2007, 2:10:25 AM4/4/07
to
Hi

I had a simple send/expect script that shoots up the CPU usage to 100%
on Unix server.

The script looks like this:
While {1} {
expect {
a {
.
exp_continue
}
b {
.
exp_continue
}
}
}

I understand that while loop could have been avoided but somehow the
user wrote the script like this. Somebody pointed out that using send/
expect in a while loop is causing this behavior (excess CPU
utilization) but I'm not sure about the logic behind this.

Expert advice needed.

Regards
Sharad

Cameron Laird

unread,
Apr 4, 2007, 11:18:22 AM4/4/07
to
In article <1175667025.0...@b75g2000hsg.googlegroups.com>,
.
.
.
What's the question?

Someone else wrote this. You're already aware that it's not
recommended style. You want ... well, what is it that you
want? I suspect you're trying to ask for the same behavior,
but with a lower CPU load, but I don't understand what
behavior (written by a third person) you're trying to preserve.

My summary advice: don't do that; decide what you do want to
do.

Sharad

unread,
Apr 5, 2007, 8:03:23 AM4/5/07
to
I was just curious to know as to what is the reason behind CPU load
shooting up to 100% for something that appears as simple as executing
a while loop containing a send/expect block ?
Or if this is not the reason for the CPU overload, what else could be
the reason ?
The send/expect block was intended for username/password/issue
commands on a router.
while {1} {
expect {
-re "Username.*:" {
send "abc\r"
exp_continue
}
-re "Password.*:" {
send "xyz\r"
exp_continue
}
-re "$prompt" {
send "router command show"
exp_continue
}
}
after 1000
}

The script intended to crash the router - that was the test objective.

Any thoughts.

Regards
Sharad


Bezoar

unread,
Apr 12, 2007, 3:44:00 PM4/12/07
to

I tried your script on my linux box. The only way I could repeat
your CPU usage was to set the timeout to 0 and to insert the timeout
handler in the expect statement ( script below: I should mention that
as written your program has no
way to get out of the inner expect command as this is in effect a
while loop. You have a while loop within a while loop! When you call
exp_continue it does not go to the outer while but back to the expect
statement to start expecting all over again. Thus if you actually
succeed in logging in and issuing the command
"router command show" you will get back the output of that command
plus THE PROMPT
which expect matches and it issues the "router command show" again ad
infinitum. Thus
you spin up the CPU. At no time does it leave the expect loop and so
will never delay 1 second like it appears you want it to. Remove the
exp_continue from the clause matching
the prompt and you will get the behaviour I believe you want. Some
advice ... always provide the eof and timeout handlers in an expect
statement and call exp_wait() and exp_close() to clean up otherwise
expect will run out of handles and/or your system will fill up with
zombies.

Carl

set prompt {.*[>\#\$]}
set timeout 0


while {1} {
expect {
-re "Username.*:" {
send "abc\r"
exp_continue
}
-re "Password.*:" {
send "xyz\r"
exp_continue
}
-re "$prompt" {
send "router command show"
exp_continue
}

timeout {
exp_continue;
}
}
after 1000

}

Cameron Laird

unread,
Apr 27, 2007, 8:03:08 AM4/27/07
to
In article <1176407040....@y5g2000hsa.googlegroups.com>,

This one has me curious. sharad, can you supply any more detail?
To me, too, this problem is inscrutable--your report of the
symptoms just doesn't match what I'm able to reproduce.

0 new messages