SFTP files upload and download automation in Linux using shell script

SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream

SFTP server default port is 22.

sftp command syntax : (man sftp)

sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config] [-o ssh_option] [-P sftp_server_path] [-R num_requests] [-S program] [-s subsystem | sftp_server] host
sftp [user@]host[:file …]
sftp [user@]host[:dir[/]]
sftp -b batchfile [user@]host

Use full sFTP Interactive commands:

pwd — to know the current remote working directory

lpwd –  to know the current local working directory

ls  – to display remote files list

lls – to display local files list

cd  — to change remote directory

lcd – to change local directory

get — to download remote file to local

mkdir — create a directory of sftp server

put — to upload files to sftp server.

rm — to remove files in the remote directory.

bye — to log out from sftp server.

mput — to upload multiple files

mget — to download multiple files from sftp

 

Upload Files

Install lftp command using with yum utility

yum install lftp

Using sFTP Password. 

lftp<<END_SCRIPT
open sftp://<sftpIp>
user <sftpUserName> <sftppwd>
cd <sftppath>
put  <File Name>
bye
END_SCRIPT

 

Using .pem Identify File.

(
echo ”
cd <remote-destination-path>
lcd <local-source-path>
put <filename>
ls
quit

) | sftp -o “IdentityFile=<Identify FIle>.pem” <User Name>@<IP Address>

Download files from SFTP: 

Using sFTP Password. 

lftp<<END_SCRIPT
open sftp://<sftpIp>
user <sftpUserName> <sftppwd>
cd <sftppath>
get  <File Name>
bye
END_SCRIPT

Using .pem Identify File.

(
echo ”
cd <remote-destination-path>
lcd <local-source-path>
get <filename>
quit

) | sftp -o “IdentityFile=<Identify FIle>.pem” <User Name>@<IP Address>

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s