How to run ssh command until succeeded?

Another option would be to use until.

until ssh [email protected]; do
    sleep 5

If you do this repeatedly for a number of hosts, put it in a function in your ~/.bashrc.

read -p "Enter the hostname or IP of your server :" servername
until ssh $servername; do
    sleep 5

ssh [email protected]
until !!; do sleep 5 ; done

The !! to repeat the last command.

OpenSSH has a ConnectionAttempts setting that does almost what you want. The default is 1 but you can change it in ssh_config or on the command-line:

ssh -o 'ConnectionAttempts 10' ...

Unfortunately you can't adjust the attempt interval which is fixed at 1 second, but you can adjust the connection attempt time-out ConnectTimeout (in seconds) in the same fashion.