SFTP files to a 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.

man expect

Using expect is not the way to go really. Can you consider setting up SSH keys? If you can exchange them, you can get sftp to sign on without prompting. You can also then use the -b flag to batch up the commands you want to run with the sftp session.

Does this help?
Robin

There's a reason hardcoding passwords in scripts is so hard, and why things like passwordless SSH key exchange were developed.

It's insecure.

Use SSH keys. It's why they've been created.