We recently had to create a cron job that downloads some files over SFTP, which is different from the similarly named FTPS.

SFTP (Secure File Transfer Protocol) uses SSH to ensure the security of file transfers, unlike FTPS, which uses SSL.

The problem with downloading files over SFTP using a cron job, however, is that password-based authentication, which is what we were using to log into the SFTP server, normally only works in an interactive shell, where the user can physically type the password into the terminal. But shell scripts run by a cron job are non-interactive.

One way to get around this is to try passwordless authentication using SSH keys.

But let’s assume we really, really want to use password-based authentication.

We’re in luck: There are some ways to emulate an interactive shell using a script.

Below, we’ll present two approaches, both in Python. Each of these scripts can be run in a cron job.

Using the pexpect module

More info about pexpect is at [http://www.noah.org/wiki/pexpect].

Using the paramiko module

More info about paramiko is at [http://www.lag.net/paramiko/].

Clearly, using the paramiko module is the simpler approach, although if you are already using the pexpect module for other things and don’t want to go through the small trouble of installing paramiko, you may wish to use the first script.