This tutorial aims to detail step by step shell script to transfer files using sftp on UNIX systems.
In this tutorial we want to transfer a .jpeg file from a directory on a local computer to a directory on a remote server.
We have to use SFTP to secure the file and transfer mode.
copy multiple files
xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.
sftp is mostly for interactive operations, similar to ftp, which performs all operations over an encrypted ssh transport. It may also use many features of ssh, such as public key authentication and compression. sftp connects and logs into the specified host, then enters an interactive command mode. You need to specify host you want to connect to:
you will be prompted for username and passsword, and the interactive session will begin..
Although it can be used in scripts, the
scp is much more easy to use:
scp /path/to/localfile user@host:/path/to/dest
you will be prompted for password.
ssh as underlying protocol.
The best way to setup them to run from scripts is to setup passwordless authentication using keys.
Setup passwordless authentication using keys
In this example we will setup SSH password-less automatic login from server 192.168.0.12 as user tecmint to 192.168.0.11 with user yoctobe.
Step 1: Create Authentication SSH-Kegen Keys on – (192.168.0.12)
First login into server 192.168.0.12 with user tecmint and generate a pair of public keys using following command:
[email@example.com ~]$ ssh-keygen -t rsa
Step 2: Create .ssh Directory on – 192.168.0.11
Use SSH from server 192.168.0.12 to connect server 192.168.0.11 using yoctobe as user and create .ssh directory under it, using following command:
[tecmint@tecmint ~]$ ssh firstname.lastname@example.org mkdir -p .ssh
Step 3: Upload Generated Public Keys to – 192.168.0.11
Use SSH from server 192.168.0.12 and upload new generated public key (id_rsa.pub) on server 192.168.0.11under yoctobe‘s .ssh directory as a file name authorized_keys.
Step 4: Set Permissions on – 192.168.0.11
Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file
[tecmint@tecmint ~]$ ssh email@example.com "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
Step 5: Login from 192.168.0.12 to 192.168.0.11 Server without Password
From now onwards you can log into 192.168.0.11 as yoctobe user from server 192.168.0.12 as tecmint user without password.
[tecmint@tecmint ~]$ ssh firstname.lastname@example.org
After you setup keys, you can run
scp -i private-key-file /path/to/local/file user@host:/path/to/remote
sftp -oIdentityFile=private-key-file -b batch-file user@host
If you want to authenticate with password, you may try the
expect package. The simplest script may look like this:
#!/usr/bin/expect spawn sftp -b batch-file user@host expect "*?assword:*" send "pasword\n" interact