While executing a shell script, my script needs to login to another server with ssh connect (need to give correct password also) and execute some steps of the script in that server, then come back again to home server. How can I do this?

  • You should not supply a password to your ssh client but rather set up a (passwordless) ssh key and use that to Authenticate instead – HBruijn Apr 2 '18 at 16:41

The proper solution to accomplishing this goal is to use SSH Keys when authenticating to the remote end-point. This removes the requirements to using a password, but still provides a secure method for executing commands via SSH on the remote system. There are many good tutorials on the internet to accomplish this, Digital Ocean has one for instance.

The basic steps are to use ssh-keygen to create the key-pair, then copy the public key to the remote system. The remote system typically would have a .ssh folder in the user's home directory. In that folder, you can create a file authorized_keys where you would paste the contents of the public key generated earlier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.