This question already has an answer here:

We have the base64 encoded SSH key password (so not a user account password!).

We have to keep an SSH tunnel opened to a place.

But sometimes the connection is bad and the SSH session terminates.

When we start the SSH tunnel, the SSH private key is encrypted with a password, thus requiring a human to input the pw.

We know that it is just obscurity to have the private ssh key password in a file, but it helps :)

Question: How can we automate the pw input when we open the tunnel? Maybe "expect" can help?

marked as duplicate by Jakuje, GAD3R, Satō Katsura, Gilles ssh Mar 14 '17 at 23:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    try looking for ssh-agent. this will basically load, with human entered password a key "in memory", then each time you'll shh to a place, this key might be used. – Archemar Mar 14 '17 at 6:04
  • You can also try sshpass cyberciti.biz/faq/… – Zumo de Vidrio Mar 14 '17 at 8:32

First, is a best solution use a private/public key to connect automatically without password, if the connection loss, you can use autossh to keep alive. Follow this instructions. You can create a SSH key pair with the following command in client, keep password empty, to connect without password:

ssh-keygen -t rsa -b 4096

Then, copy the SSH key to the server with the command:

ssh-copy-id user@server.es

Now you can connect with server without password. Finally install autossh and execute the connection to server to create the tunnel:

autossh -M 0 -fN -o "ServerAliveInterval 30" -i /path/id_rsa -L 3307:localhost:3306 user@server

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