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

using Expect as path in pipe

153 views
Skip to first unread message

A Ferenstein

unread,
Jul 1, 2004, 5:06:05 AM7/1/04
to
I have following requirement:

(awk program) | (Expect program which spawns Telnet) | (awk program)

'|' above are pipes.

I need the Expect program to send the stuff that's coming-in on STDIN via
Telnet and print the reply on STDOUT.
Is this possible?


Don Libes

unread,
Jul 1, 2004, 8:46:08 PM7/1/04
to
"A Ferenstein" <epa...@hotmail.com> writes:

Not only is it possible, it's trivial. Here's the whole script:

spawn -noecho telnet ....
interact

-Don

A Ferenstein

unread,
Jul 4, 2004, 10:21:21 PM7/4/04
to
> spawn -noecho telnet ....
> interact

I'm just a beginner, so I don't understand...

Here's my script2.exp (mainly generated by autoexpect):

$ cat script2.exp
#!/usr/bin/expect -f

set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout -1
spawn telnet 146.11.28.64 3300
match_max 100000
expect -exact "Trying 146.11.28.64...\r
Connected to 146.11.28.64.\r
Escape character is '^\]'.\r
?Enter command: "
send -- "LOGIN:root:root_password;\r"
expect -exact "LOGIN:root:root_password;\r
RESP:0;\r
?Enter command: "
send --
"GET:OM_JAS:DNAME,JAMBALA/JPTSCP1/JAS:CHILDREN,OM_JAS_PRINCIPALSUBSCRIBER;\r
"
expect "command: "
send -- "LOGOUT;\r"
expect -exact "LOGOUT;\r
RESP:0;\r
?Enter command: "

Now run script2.exp, all OK:

$ expect -f script2.exp
spawn telnet 146.11.28.64 3300
Trying 146.11.28.64...
Connected to 146.11.28.64.
Escape character is '^]'.
?Enter command: LOGIN:root:root_password;
RESP:0;
?Enter command:
GET:OM_JAS:DNAME,JAMBALA/JPTSCP1/JAS:CHILDREN,OM_JAS_PRINCIPALSUBSCRIBER;
RESP:0:CHILDREN,"[Ref to OM_JAS_PRINCIPALSUBSCRIBER::
JAMBALA/JPTSCP1/JAS/REFERENCEID=100001]","[Ref to "[Ref to
OM_JAS_PRINCIPALSUBSCRIBER:: JAMBALA/JPTSCP1/JAS/REFERENCEID=FF9]";
?Enter command: LOGOUT;
RESP:0;

Here's my trivial.exp with your suggestion to put "interact":

$ cat trivial.exp
#!/usr/bin/expect -f

set force_conservative 1 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout -1
spawn telnet 146.11.28.64 3300
match_max 100000
expect -exact "Trying 146.11.28.64...\r
Connected to 146.11.28.64.\r
Escape character is '^\]'.\r
?Enter command: "
send -- "LOGIN:root:root_password;\r"
expect -exact "LOGIN:root:root_password;\r
RESP:0;\r
?Enter command: "
interact

Same input as before:

$ cat get
GET:OM_JAS:DNAME,JAMBALA/JPTSCP1/JAS:CHILDREN,OM_JAS_PRINCIPALSUBSCRIBER;"

Try running together:

$ cat get | expect -f trivial.exp
spawn telnet 146.11.28.64 3300
Trying 146.11.28.64...
Connected to 146.11.28.64.
Escape character is '^]'.
?Enter command: LOGIN:root:root_password;
RESP:0;
?Enter command:

So, where's the input from get file being piped-in?


Don Libes

unread,
Jul 6, 2004, 9:43:52 PM7/6/04
to
"A Ferenstein" <epa...@hotmail.com> writes:

Ah, I see. There are a couple things going on here. The interact
command is going to notice the eof from stdin before the spawned
process gets around to replying. When interact notices the eof, it
will return. So you need an expect after the interact.

That leaves one more problem. interact closes the channel on eof and
in the current implementation, that will close stdout too making it
impossible for the script to continue as a filter. There is a way to
script around this but I've added a new command that drastically
simplifies things: close_on_eof. It takes a boolean (and optional
channel) to enable or disable the close-on-eof behavior.

So replace the interact at the end of your script with this:

close_on_eof 0
interact
expect

You'll need to get a new version expect. I've temporarily made it
available as a beta. http://expect.nist.gov/beta.tar.gz

Don

0 new messages