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
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.
The script intended to crash the router - that was the test objective.
Any thoughts.
Regards
Sharad
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
}
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.