Alex1987
Alex1987

Reputation: 9457

How to copy files to remote computer using Bash script?

I have a script that needs to copy a file to a remote computer:

cp -R "${DEST_FOLDER}" "${SRC_FOLDER}"

How can I do it when the remote computer requires user and password for access?

How do I login to this computer with a bash script?

Thanks

Upvotes: 1

Views: 9352

Answers (1)

kamituel
kamituel

Reputation: 35950

Bash itself will not let you access remote host (obviously), but you could use SSH:

Step 1: On your local PC generate key to perform password-free authentication later

$ ssh-keygen

It will ask you to enter passphrase. If you want your bash script to be fully non-interactive, you can opt not to use any password.

Step 2: Copy you public key to the remote host:

$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote-host

Step 3: Use scp to copy files:

$ scp -r local_file user@remote-host:/remote_dest_dir/

Upvotes: 6

Related Questions