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?
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.