Automating your FTP session using expect

by Amit

Shamelessly derived from a code on the Wikipedia entry for Expect, here is how you would automate a FTP session:

# Open an ftp session to a remote server, logs in using your credentials and retrieves the current
# directory listing from there

# Amit k. Saha

# This is how you set variables in 'expect'

set remote_server your-ftp-server
set my_user_id your-user-ide
set my_password your-pass

spawn ftp $remote_server
expect "username:"

# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for an ftp prompt.
send "$my_password\r"

expect "ftp>"

# list all the files
send "ls \r"

expect "ftp>"

# Exit the ftp session, and wait for a special end-of-file character.
send "bye\r"
expect eof