Bash / Expect Script

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
CrowderSoup
Forum Newbie
Posts: 18
Joined: Thu Apr 21, 2011 2:56 pm
Location: Murray, UT

Bash / Expect Script

Post by CrowderSoup »

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. http://www.tcl.tk/man/expect5.31/expect.1.html.

Here's my expect script thus far:

Code: Select all

#!/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:

Code: Select all

#!/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"
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Bash / Expect Script

Post by Weirdan »

Wouldn't scp + public key auth be a simpler solution?
Prodeo
Forum Newbie
Posts: 1
Joined: Thu Jun 30, 2016 10:04 am

Re: Bash / Expect Script

Post by Prodeo »

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.
Post Reply