scripting ftp/sftp transfer in a single line command


Do you ever find yourself in dire need to do file transfer with ftp or sftp on only a single line? You can “get” files with a single sftp command, but you can’t do “put”. A couple of way you can do this are:

  • You can use sftp in batch mode, put everything in a batch file, then execute it in a single line. Since the sftp command does not support “sshpass” you have to have key-based passwordless authentication. This is IMHO the most correct way to do these kind of stuff.
  • Or, you can use scp. But then the target server must also provide or support scp. You can either use passwordless authentication or, albeit definitely less secure, have sshpass throw the password to your scp command.
  • Or if your partner’s server is finicky about having you use key-based authentication and suport only sftp and sftp only. You can use lftp.

lftp is a complex file transfer application that support many protocols. For the case above, you can “put”  a file to a server, using sftp, with password authentication by doing:

 lftp sftp://user:password@www.xxx.yyy.zzz -e "cd /incoming/; put transaction-(date +%Y%m%d).txt'; bye"

The line above will open an sftp session to http://www.xxx.yyy.zzz, go to a remote directory /incoming/ on the target server, and put transaction-yyy-mm-dd.txt into the directory, and then disconnects.

Simple isn’t it? Do remember that you’re putting the password in a cleartext format, so make sure that you and only you can read and execute the script.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s