Shell script to sftp files to file server.

Hi,

I am looking for a shell script to sftp to a file server and copy all the files from a directory after the script is run.
The server name should be a user input parameter and of-course the username/password as well. Rest all should be handled by the script.

I tried with below snippet:-

echo "FTP'ing the CSV output to file server now.."

echo "Please enter the FTP server name:"
read ftpservername

echo "Please enter the username:"
read username

sftp $username@$ftpservername

However, the problem is:- When it is run, it brings on the sftp password prompt - which I dont want. I would like to handle password as well automatically within script as user input and then copy files from a particular directory to a directory on the file server (This is also a user input).

Any idea how this can be handled with-in the script? The script should automatically take care of establishing a successful SFTP session to the server and then transfer the files.

Help with sample script will be highly appreciated.

Thanks.

Create an ssh key with ssh-keygen ! More info:

man ssh-keygen

sftp is optimized for interactive use. Perhaps you can copy the files with scp ? Both use the ssh protocol.

after keygen it will generate 2 files example id_dsa and id_dsa.pub

you need to copy the public key to remote server you want to login rename the file to be called authorized_keys (remove all end of line control characters)

it should work on unix and linux but becareful of windows and maybe aix as they use something called tectia ssh the method is similar but syntax is different I forget already how it is.

1 Like