Automating FTP transfers on Linux

I was recently working on moving a lot of files between servers, but was hugely constrained by the fact that the servers had only FTP to work with. Even though I understand that it’s not secure AT ALL, there was nothing I could do but work on automating FTP transfers on linux. For FWIW, if you have the choice, DO NOT use FTP to transfer files between servers. You can use tools like rSync which make this process easy. Now that the disclaimers are out of the way, let’s dive in.

Any FTP client basically operates by issuing a bunch of commands to the remote servers in a sequence. The basic idea is like this:

FTP CLIENT> Open connection to server
FTP CLIENT> This is my username and password (notice how it’s not encrypted in any way, and thus is open to getting sniffed by any one monitoring the traffic between your machine and the destination machine, which is why it’s insecure)
FTP CLIENT> Move to directory of interest
FTP CLIENT> Issue a “get” command to download files to your local computer
FTP CLIENT> Finally say “KTHXBAI”. 🙂

Let’s look at a file that issues these commands and assume it’s saved as ftp_commands.txt

Now let’s use the ftp command and actually run it:

The -n modifier is the one that does the trick here. It actually prevents ftp from attempting to automatically login upon issuing the “open” command. In this mode, ftp commands expects the user name and password, which we feed to it right after the open command and we’re able to login to the server.

Leave a Reply

Your email address will not be published. Required fields are marked *