#!/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
#
> 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 > "
}
Asif gives good advice. My personal bias is to leave the
"Slow down ..." part until you've corrected everything else.
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