0

I want to write a bash script to connect to my remote server and do some file transfer.

But because connection prompts for username and then hit enter, and then it requires the password.

How can I use the bash script to automatically enter these things?

For example echo username; | ssh xxxx@XXX.edu this does not work.

vanadium
  • 88,010
echo Lee
  • 103

1 Answers1

3

One of the easiest ways would be to add your key to the remote host. To add your key to the remote host so that ssh / scp does not require a password you can run:

ssh-copy-id username@host.edu

Then you can use scp to copy from the remote to the client system in the current folder you are in:

scp username@host.edu:/path/to/file/filename .

Or copy it to any folder that you have write access to on your client host:

scp username@host.edu:/path/to/file/filename /local/folder/to/store/in/

Another way to do it would be to install sshpass (sudo apt install sshpass) on your client system. Then you can use it to log into the remote host and run the commands that way. One thing to remember that storing a password in a script can be read by anyone that has access to the script.

To use sshpass see https://askubuntu.com/a/385850/231142

Terrance
  • 41,612
  • 7
  • 124
  • 183