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 [email protected]
this does not work.
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 [email protected]
Then you can use scp to copy from the remote to the client system in the current folder you are in:
scp [email protected]:/path/to/file/filename .
Or copy it to any folder that you have write access to on your client host:
scp [email protected]:/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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments