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

Expect programming help w/power switch

14 views
Skip to first unread message

John Serrao

unread,
Aug 5, 2009, 12:27:18 PM8/5/09
to
Hi All, I have been trying to get an Expect program to communicate
with a Remote Power Switch which will eventually log in, and then turn
on/off certain outlets. For some reason, I can't get through the log
in procedure with this Switch. When you first spawn the telnet, you
get the Switch info/header, and then a Password prompt, but for some
strange reason, the first time you send the password, it gets ignored,
and repeats the whole header.. ok, but I try to send the password
again, and the Expect script sits there and eventually times out.. I
ripped out the log in procedure and made it very simple just to try to
get through the log in procedure to get to the main menu of the
switch, even this small piece of code doesn't work... So, I'm a bit
frustrated.. Maybe it's been too much for my eyes and it's a trivial
problem but here is the piece of code... Can anyone help make heads or
tales of why I can't get past this darn point. When the log in
procedure is successful, I should get the
Enter > prompt, but I can't get here. Thanks in advance...

#!/opt/ActiveTcl-8.4/bin/tclsh


package require Expect

log_user 1
exp_internal 1

set timeout 20

spawn telnet 1XX.1XX.1XX.149

expect "*Password > "

after 1000

send "PASS\r"

expect "*Enter > "


And here is the output I get with the debug levels turned up:

# ./johnTest
spawn telnet XXX.112.XXX.149
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {27205}

expect: does "" (spawn_id exp4) match glob pattern "*Password > "? no
Trying XXX.112.XXX.149...

expect: does "Trying XXX.112.XXX.149...\r\n" (spawn_id exp4) match
glob pattern
"*Password > "? no
Connected to XXX.112.XXX.149.
Escape character is '^]'.

expect: does "Trying xXx.112.XXX.149...\r\nConnected to XXX.112.XXX.
149.\r\nEsc
ape character is '^]'.\r\n" (spawn_id exp4) match glob pattern
"*Password > "?
no


iPal Version 2.3e
Please Enter The Password >
expect: does "Trying XXX.112.XXX.149...\r\nConnected to XXX.112.XXX.
149.\r\nEsc
ape character is '^]'.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n
\r\n\r\n\
r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\niPal Version 2.3e\r\n
\r Please
Enter The Password > " (spawn_id exp4) match glob pattern "*Password >
"? yes
expect: set expect_out(0,string) "Trying XXX.112.XXX.149...\r
\nConnected to XXX
.112.XXX.149.\r\nEscape character is '^]'.\r\n\r\n\r\n\r\n\r\n\r\n\r\n
\r\n\r\n\
r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r
\niPal Versio
n 2.3e\r\n\r Please Enter The Password > "
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "Trying XXX.112.XXX.149...\r\nConnected
to XXX.1
12.XXX.149.\r\nEscape character is '^]'.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r
\n\r\n\r\
n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\niPal
Version
2.3e\r\n\r Please Enter The Password > "
send: sending "PASS\r" to { exp4 }

expect: does "" (spawn_id exp4) match glob pattern "*Enter > "? no
PASS

expect: does "PASS\r\n" (spawn_id exp4) match glob pattern "*Enter >
"? no
expect: timed out
#


Asif

unread,
Aug 5, 2009, 6:00:04 PM8/5/09
to
I'm going to point out some clues in your debug log:

> iPal  Version 2.3e
>  Please Enter The Password >
> expect: does "Trying XXX.112.XXX.149...\r\nConnected to XXX.112.XXX.
> 149.\r\nEsc
> ape character is '^]'.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n
> \r\n\r\n\
> r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\niPal  Version 2.3e\r\n
> \r Please
> Enter The Password > " (spawn_id exp4) match glob pattern "*Password >
> "? yes

Notice that the pattern matched the long string ending in "...Enter
The Password > "!

> expect: set expect_out(0,string) "Trying XXX.112.XXX.149...\r
> \nConnected to XXX
> .112.XXX.149.\r\nEscape character is '^]'.\r\n\r\n\r\n\r\n\r\n\r\n\r\n
> \r\n\r\n\
> r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r
> \niPal  Versio
> n 2.3e\r\n\r Please Enter The Password > "
> expect: set expect_out(spawn_id) "exp4"
> expect: set expect_out(buffer) "Trying XXX.112.XXX.149...\r\nConnected
> to XXX.1
> 12.XXX.149.\r\nEscape character is '^]'.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r
> \n\r\n\r\
> n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\niPal
> Version
> 2.3e\r\n\r Please Enter The Password > "
> send: sending "PASS\r" to { exp4 }
>
> expect: does "" (spawn_id exp4) match glob pattern "*Enter > "? no
> PASS

Notice how a match is being attempted on ""!
Where did the "" come from?

> expect: does "PASS\r\n" (spawn_id exp4) match glob pattern "*Enter >
> "? no
> expect: timed out
> #

Now notice how a match is being attempted on "PASS\r\n".
But "PASS\r\n" was passed by your script to telnet!?!

You *may* need to:
- Slow down your script's interaction with telnet.
- Send a "\r" after sending "PASS\r"
- Omit the after 1000 delay.
Try nesting the send within expect like so:

set send_slow {2 .1} ;# Interact with telnet slowly.


set timeout 20
spawn telnet 1XX.1XX.1XX.149

expect {
-glob "*Password > " {
after 1000 ;# You might want to omit this.
send -- "PASS\r"
send -- "\r" ;# Try sending another carriage
return separately.
expect -glob -- "*Enter > "
}

Cameron Laird

unread,
Aug 5, 2009, 8:47:14 PM8/5/09
to
In article <fe78e16a-fd71-4e64...@b25g2000prb.googlegroups.com>,

Asif <asif.h...@gmail.com> wrote:
>I'm going to point out some clues in your debug log:
.
.
.

>Notice how a match is being attempted on ""!
>Where did the "" come from?
>
>> expect: does "PASS\r\n" (spawn_id exp4) match glob pattern "*Enter >
>> "? no
>> expect: timed out
>> #
>
>Now notice how a match is being attempted on "PASS\r\n".
>But "PASS\r\n" was passed by your script to telnet!?!
>
>You *may* need to:
>- Slow down your script's interaction with telnet.
>- Send a "\r" after sending "PASS\r"
>- Omit the after 1000 delay.
>Try nesting the send within expect like so:
>
>set send_slow {2 .1} ;# Interact with telnet slowly.
>set timeout 20
>spawn telnet 1XX.1XX.1XX.149
>expect {
> -glob "*Password > " {
> after 1000 ;# You might want to omit this.
> send -- "PASS\r"
> send -- "\r" ;# Try sending another carriage
>return separately.
> expect -glob -- "*Enter > "
>}

Asif gives good advice. My personal bias is to leave the
"Slow down ..." part until you've corrected everything else.

John Serrao

unread,
Aug 6, 2009, 10:29:11 AM8/6/09
to

Thanks Much Guys... I think it was a combination of the slowing down
the sending of the password, and cleaning up my phrases to Expect upon
after the send that finally got it to work!! I always get confused
with the "-re" vs. "-glob" vs. no arguments when using the Expect
term.

Thanks again for your help in deciphering the debug output.

john

0 new messages