I need to write an expect script that connects to an FTP server on the command line. I need to traverse into a specific folder and download all the files in a certain directory and delete them when I'm done downloading them.
I can do this without expect using
Code:
ncftp -u username ftp.domain.com
cd downloads
get *.tgz
I am trying to solve two different problems.
The first is, I need to get a list of the files in the remote directory, and iterate over them, downloading each one and deleting it.
The second problem is, I need the script to not timeout. I figure once I have switched my stupid code below into a
foreach, that's the perfect place to put exp_continue, but I am not sure how to code getting the file list and iterating on the files.
Where I'm stuck is this:
gives me a nice file list, but I have to send "ls -a" and store the resulting list in an array inside expect.
How do you store a list of files returned from an FTP server in an array inside expect?
Here's what I have so far...
Code:
#!/usr/bin/expect
set timeout 300
# @TODO Change to a argv
cd /home/myusername/downloads
# Start ncftp
spawn ncftp -u $ftpusername $ftphost
expect "Password:"
# Send the password
send "$password\r"
# Get an FTP prompt
expect "*$ftpusername*"
# Change directories
send "cd downloads\r"
# Moved into directory
expect "*successfully changed*"
# Get an FTP prompt
expect "*downloads*"
# here's where I need a loop
send "ls -a\r"
send "get *.tgz\r"
expect "100%"
# exp_continue
# end loop
expect "*outgoing*"
send "bye"
puts "All done.\r"