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.
> #!/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
So: when diagnosing Expect situations, be on the way for
simpler, but Tcl-savvy, ways to make progress.
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
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!
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 >.
but there is a tclwrapper for curl tclcurl: http://wiki.tcl.tk/2638
uwe
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
all in bits and pieces,
exec curl http://wiki.tcl.tk/4
works for me ;-)
uwe