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 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.

By ikhsan

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.