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

Expect script help

261 views
Skip to first unread message

Trevor

unread,
Jan 19, 2006, 7:03:36 PM1/19/06
to
I'm new to expect and am trying to write a script that has multiple
spawn calls, the script works except that the second spawn is executing
before the first one which causes it to fail. How do i tell it to wait
until the current spawn process has finished?

Here is a basic outline of what I'm trying to accomplish...

1. Use curl to call a secure url on local machine which triggers a
script to export data to a file.

2. SFTP the newly generated file to another machine

3. delete the original file...

So as you can see, each step needs to finish before the next is
executed. How do I do this? I can do it using "sleep" but that's not
elegant and isn't bulletproof.

Any help/tips etc would be much appreciate.. Here is my (probably
horrendous) script

-------------------------------------------

#!/usr/local/bin/expect

# Export data from website...
spawn curl -v -u username:password http://www.domain.com/
set timeout -1
sleep 30

spawn sftp us...@123.123.123.123
expect "Password:"
send "password\n";
expect "sftp> "
send "lcd /path/to/file\n"
expect "sftp> "
send "put myfile.txt\n"
expect "sftp> "
send "exit\n"

sleep 30

spawn rm /path/to/file/myfile.txt

interact

------------------
Thanks.

Uwe Klein

unread,
Jan 20, 2006, 3:31:56 AM1/20/06
to
Hi Trevor,
You wrote:

> #!/usr/local/bin/expect
>
> # Export data from website...
> spawn curl -v -u username:password http://www.domain.com/

# either you wait on curl getting its job done and exiting:
expect eof ; close ; wait
# or ( as curl is not relly interactive )
# are you bent on using spawn?
# what about
exec curl -v -u username:password http://www.domain.com/
# returns after curl is finished

> set timeout -1
# you are at work, no sleeping!


> sleep 30
>
> spawn sftp us...@123.123.123.123
> expect "Password:"
> send "password\n";
> expect "sftp> "
> send "lcd /path/to/file\n"
> expect "sftp> "
> send "put myfile.txt\n"
> expect "sftp> "
> send "exit\n"

expect eof ; close ; wait
>
# s.o.


> sleep 30
>
> spawn rm /path/to/file/myfile.txt
>
> interact
>
> ------------------
> Thanks.

uwe

Cameron Laird

unread,
Jan 20, 2006, 9:08:01 AM1/20/06
to
In article <soj6a3-...@robert.houseofmax.de>,

Uwe Klein <uwe_klein_...@t-online.de> wrote:
>Hi Trevor,
>You wrote:
>
>> #!/usr/local/bin/expect
>>
>> # Export data from website...
>> spawn curl -v -u username:password http://www.domain.com/
># either you wait on curl getting its job done and exiting:
>expect eof ; close ; wait
># or ( as curl is not relly interactive )
># are you bent on using spawn?
># what about
>exec curl -v -u username:password http://www.domain.com/
.
.
.
*Many* Expect users, in my experience, aren't aware that
[exec], for example, is available to them. More broadly,
it's characteristic that Expect users are aware of only
an unusually slender fraction of Tcl's capabilities--the
first couple chapters of The Book, say, with absolutely
no conception that so much more is possible. I don't
intend this as criticism at all; it's just a symptom of
the way knowledge about Expect has been spread, and
through whom.

So: when diagnosing Expect situations, be on the way for
simpler, but Tcl-savvy, ways to make progress.

Trevor

unread,
Jan 20, 2006, 4:32:03 PM1/20/06
to
Thanks. I actually tried using exec to call curl, but it would seem to
execute the curl portion but the rest of the script wouldn't be executed.

I tried to add this after you mentioned it again but it gave me the same
response.

Also, with this line: expect eof ; close ; wait

is that meant to be as it is writtn? Or would just "expect eof;"
suffice? I ask because that's what I'm using now because when I had
that whole line in there it gave me an error. (I can't remember right
off... I was playing with it last night...

Anther questions I have is: Does the command "set timeout" alwasys
pertain to the whole expect script or does it pertain to specific
regions? such as to set a timeout solely for the sftp call... I imagine
that is the case..

Below is my scipt as it is now. It may not be most elegant way of doing
it, but it works...

---------------------------------------

#!/usr/bin/expect

###########################################


# Export data from website...

###########################################

spawn curl -u username:password http://www.domain.com/myscript
expect eof

# exec curl -u username:password http://www.domain.com/myscript
# If I uses exec instead of the spawn/expect lines above it
# seems to stop here and the rest of the script isn't executed.

###########################################
# Upload export file to Client Server
###########################################

spawn sftp us...@server.com
expect "password:"


send password\n";
expect "sftp> "
send "lcd /path/to/file\n"
expect "sftp> "

send "put test.txt\n"


expect "sftp> "
send "exit\n"
expect eof

###########################################
# Delete the file that was just uploaded
###########################################

exec rm /path/to/file/test.txt

Trevor

unread,
Jan 20, 2006, 4:48:07 PM1/20/06
to


I was actually aware of exec I've been using "man expect" as my primary
guide, but I have also been bumbling around google a bit...

This is my first expect script. I'm excited to have learned about this
new tool! What is "The Book" you are refering to? Exploring Expect?
(just found it mentioned on the web... haven't seen it...)

To me it seems like expect has been a little known secret -- In the past
I have ended up with convoluted code and hacks to try and do what expect
seems to do so easily. I wish I'd found it a lot sooner!

Cameron Laird

unread,
Jan 20, 2006, 6:08:02 PM1/20/06
to
In article <r82dnRTBjYM...@bright.net>,
Trevor <trevorb...@yahoo.com> wrote:
.
.

.
>This is my first expect script. I'm excited to have learned about this
>new tool! What is "The Book" you are refering to? Exploring Expect?
>(just found it mentioned on the web... haven't seen it...)
>
>To me it seems like expect has been a little known secret -- In the past
>I have ended up with convoluted code and hacks to try and do what expect
>seems to do so easily. I wish I'd found it a lot sooner!
>

1. I know the feeling--and I've been using it since
the mid-'90s.
2. Expect is hilariously old. I understand it's
new to you. That's OK. Expect pegs the needle
on ratio of usefulness-to-notoriety.
3. Yes, *Exploring Expect* <URL: http://wiki.tcl.tk/105 >.
4. "... little-known secret ..." Hmmmm. It's as
though someone should write an article with
that title <URL:
http://www.samag.com/documents/s=9035/sam0402web1/ >.
5. <URL: http://wiki.tcl.tk/2?expect >.

Uwe Klein

unread,
Jan 21, 2006, 6:29:14 AM1/21/06
to
Trevor wrote:
> Thanks. I actually tried using exec to call curl, but it would seem to
> execute the curl portion but the rest of the script wouldn't be executed.
I am not going to take your new Toy!

but there is a tclwrapper for curl tclcurl: http://wiki.tcl.tk/2638

uwe

Uwe Klein

unread,
Jan 21, 2006, 6:50:58 AM1/21/06
to
Trevor wrote:
> Thanks. I actually tried using exec to call curl, but it would seem to
> execute the curl portion but the rest of the script wouldn't be executed.
>
> I tried to add this after you mentioned it again but it gave me the same
> response.
>
> Also, with this line: expect eof ; close ; wait

Ups, leave out the [close] because you've already seen the "eof" and
thus the close was automatic.

on timeouts:

expect -i $spawn_id -timeout $mytimeout


uwe

Uwe Klein

unread,
Jan 21, 2006, 6:58:10 AM1/21/06
to
Trevor wrote:
> Thanks. I actually tried using exec to call curl, but it would seem to
> execute the curl portion but the rest of the script wouldn't be executed.

all in bits and pieces,

exec curl http://wiki.tcl.tk/4

works for me ;-)

uwe

0 new messages