PHP Developers Network
http://forums.devnetwork.net/

Bash / Expect Script
http://forums.devnetwork.net/viewtopic.php?f=14&t=129530
Page 1 of 1

Author:  CrowderSoup [ Tue Apr 26, 2011 10:31 am ]
Post subject:  Bash / Expect Script

Hey everyone, I'm currently working on a Bash / Expect script that will sftp to a server, pull down some .txt files, run some commands, and then exit. Right now I'm actually having problems with the Expect piece of the script.

It all works fine until I run the mget command. For some reason it only gets the first file, and then part of the second. I'm not sure why this happens and haven't found much on my own in the Expect Man Page. .

Here's my expect script thus far:
Syntax: [ Download ] [ Hide ]
#!/usr/bin/expect
spawn sftp user@someserver.com
expect "Password:"
send "Some_Pass\n";
expect "sftp>"
send "cd fromcheetah\n"
expect "sftp>"
send "mget *\n"
expect "sftp>"
send "exit"
 



Any suggestions on how I can make sure that I get ALL the files before expect exits?


UPDATE:
Finally found the solution. In passing the Expect Man Page says that the default timeout for "expect" is 10 seconds. I just had to set that to a longer time so that it would wait for the file transfer to finish. The correct script is below:
Syntax: [ Download ] [ Hide ]
#!/usr/bin/expect
spawn sftp user@someserver.com
expect "Password:"
send "Some_Pass\n";
expect "sftp>"
send "cd fromcheetah\n"
expect "sftp>"
send "mget *\n"
set timeout 10000
expect "sftp>"
send "exit"
 

Author:  Weirdan [ Tue Apr 26, 2011 2:15 pm ]
Post subject:  Re: Bash / Expect Script

Wouldn't scp + public key auth be a simpler solution?

Author:  Prodeo [ Thu Jun 30, 2016 10:09 am ]
Post subject:  Re: Bash / Expect Script

Thank you for posting this. Couldn't figure out why my ftp script was dying after transferring a number of files. You're a lifesaver.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/